使用 Openiddict Core 查找具有不记名令牌的用户
Look up the user with bearer token with Openiddict Core
我目前正在使用 Openiddict、Identity 和 Entity Framework 来管理我的用户并向用户分配 Bearer
令牌以保护我的 API。
我的基础架构目前在后端使用 ASP.NET Core Web API,在前端使用单独的 React 应用程序。 React 应用程序对我的 API 进行 HTTP 调用以检索它的数据。后端根本没有服务器端 HTML 渲染。
在大多数情况下,一切都按照我的需要进行。我可以注册用户并从 API 检索令牌。这些令牌包含在我的 HTTP 调用中 Authorization
header。我的 AuthorizationController
使用这个:https://github.com/openiddict/openiddict-samples/blob/dev/samples/PasswordFlow/AuthorizationServer/Controllers/AuthorizationController.cs with a few minor tweaks. My Startup.cs
also uses almost exactly this https://github.com/openiddict/openiddict-samples/blob/dev/samples/PasswordFlow/AuthorizationServer/Startup.cs
在某些情况下,我需要 API 调用特定于用户的端点。例如,如果我需要知道用户是否对评论进行了投票。我不想在查询字符串中传递用户 ID
来获取用户详细信息,而是想使用我收到的 Bearer
令牌,他们使用该令牌对该端点进行 API 调用.不过我不确定该怎么做。
在我所做的一些研究中,看起来有些示例使用 ASP.NET 核心 MVC 而不是 API 来检索具有 User
变量的用户,如此处所示 https://github.com/openiddict/openiddict-samples/blob/dev/samples/PasswordFlow/AuthorizationServer/Controllers/ResourceController.cs#L20-L31 但这似乎不适用于我的基础设施。
我的问题是如何根据传递给 API 的 Bearer
令牌查找用户,以便从我的数据库中查找用户详细信息?我假设 API 传递的所有令牌都分配给了该特定用户,对吗?如果是这种情况,应该很容易根据 Bearer
标记查找它们。
The question is: How with Openiddict can you look up a user based on the token that was assigned to them for API calls? I need to get the user details before anything else can be done with the application first. Is there something baked internally or do I have to write my own support for this?
当您在授权控制器中创建一个 AuthenticationTicket
时(稍后 OpenIddict 使用它来生成访问令牌),您必须添加一个 sub
与 [=19] 相对应的声明=] 由访问令牌表示。
假设您使用用户标识符作为 sub
声明,您可以使用 User.FindFirst(OpenIdConnectConstants.Claims.Subject)?.Value
从 API 端点轻松提取它并使用它进行数据库查找。
我目前正在使用 Openiddict、Identity 和 Entity Framework 来管理我的用户并向用户分配 Bearer
令牌以保护我的 API。
我的基础架构目前在后端使用 ASP.NET Core Web API,在前端使用单独的 React 应用程序。 React 应用程序对我的 API 进行 HTTP 调用以检索它的数据。后端根本没有服务器端 HTML 渲染。
在大多数情况下,一切都按照我的需要进行。我可以注册用户并从 API 检索令牌。这些令牌包含在我的 HTTP 调用中 Authorization
header。我的 AuthorizationController
使用这个:https://github.com/openiddict/openiddict-samples/blob/dev/samples/PasswordFlow/AuthorizationServer/Controllers/AuthorizationController.cs with a few minor tweaks. My Startup.cs
also uses almost exactly this https://github.com/openiddict/openiddict-samples/blob/dev/samples/PasswordFlow/AuthorizationServer/Startup.cs
在某些情况下,我需要 API 调用特定于用户的端点。例如,如果我需要知道用户是否对评论进行了投票。我不想在查询字符串中传递用户 ID
来获取用户详细信息,而是想使用我收到的 Bearer
令牌,他们使用该令牌对该端点进行 API 调用.不过我不确定该怎么做。
在我所做的一些研究中,看起来有些示例使用 ASP.NET 核心 MVC 而不是 API 来检索具有 User
变量的用户,如此处所示 https://github.com/openiddict/openiddict-samples/blob/dev/samples/PasswordFlow/AuthorizationServer/Controllers/ResourceController.cs#L20-L31 但这似乎不适用于我的基础设施。
我的问题是如何根据传递给 API 的 Bearer
令牌查找用户,以便从我的数据库中查找用户详细信息?我假设 API 传递的所有令牌都分配给了该特定用户,对吗?如果是这种情况,应该很容易根据 Bearer
标记查找它们。
The question is: How with Openiddict can you look up a user based on the token that was assigned to them for API calls? I need to get the user details before anything else can be done with the application first. Is there something baked internally or do I have to write my own support for this?
当您在授权控制器中创建一个 AuthenticationTicket
时(稍后 OpenIddict 使用它来生成访问令牌),您必须添加一个 sub
与 [=19] 相对应的声明=] 由访问令牌表示。
假设您使用用户标识符作为 sub
声明,您可以使用 User.FindFirst(OpenIdConnectConstants.Claims.Subject)?.Value
从 API 端点轻松提取它并使用它进行数据库查找。