简体   繁体   English

Blazor 服务器,API 与 OpenIdDict 并使用自省 - 如何处理访问令牌?

[英]Blazor Server, API with OpenIdDict and using introspection - What to do with the access token?

I have an API that is also hosting an OpenIdDict token endpoint.我有一个 API 也托管一个 OpenIdDict 令牌端点。 The API does not have any web pages with login forms but instead returns an access token in a response as a result of receiving a form post. API 没有任何带有登录 forms 的 web 页面,而是在收到表单帖子的响应中返回访问令牌。

I previously had an old AngularJS frontend which talked to the API to get the token and stored those on the client.我以前有一个旧的 AngularJS 前端,它与 API 通信以获取令牌并将其存储在客户端上。 Angular was responsible for adding the token to every request to the server. Angular 负责将令牌添加到服务器的每个请求中。

I am now planning on rebuilding the frontend using Blazor Server.我现在计划使用 Blazor 服务器重建前端。 I want the new Blazor Server client/frontend to use introspection against the APIs token endpoint.我希望新的 Blazor 服务器客户端/前端对 API 令牌端点使用自省。

My plan was to build a custom Login page that on post would, server-side, talk to the API and get an access token, refresh token, etc. But I have no idea where to put the access token afterwards so that it's used by Blazor through introspection whenever I use the Authorize attribute.我的计划是构建一个自定义登录页面,在发布后,服务器端将与 API 对话并获取访问令牌、刷新令牌等。但我不知道之后将访问令牌放在哪里,以便它被每当我使用 Authorize 属性时,都会通过自省 Blazor。 I could just return the tokens and maybe write some javascript that saves it somewhere and adds it to any subsequent http requests, but that does not feel like a Blazor Server solution?我可以只返回令牌,也许写一些 javascript 将其保存在某处并将其添加到任何后续的 http 请求中,但这不像 Blazor 服务器解决方案?

My latest discovery is that the tokens could be stored "in session" on the server and a "session identifier" cookie is created on the client?我的最新发现是令牌可以“在会话中”存储在服务器上,并在客户端上创建“会话标识符”cookie? Might be completely off here...可能完全不在这...

When I played around with the Identity support in Blazor Server a cookie with the name ".AspNetCore.Identity.Application" was always created after a successful login.当我在 Blazor 服务器中使用身份支持时,在成功登录后总是会创建一个名为“.AspNetCore.Identity.Application”的 cookie。

Another less desirable solution, or workaround, I have been thinking about is copying the API's OpenIdDict-setup code over to the Blazor Server project and point them to the same database.我一直在考虑的另一个不太理想的解决方案或解决方法是将 API 的 OpenIdDict 设置代码复制到 Blazor 服务器项目并将它们指向同一个数据库。

Any help here would be greatly appreciated!这里的任何帮助将不胜感激!

My plan was to build a custom Login page that on post would, server-side, talk to the API and get an access token, refresh token, etc. But I have no idea where to put the access token afterwards so that it's used by Blazor through introspection whenever I use the Authorize attribute.我的计划是构建一个自定义登录页面,在发布后,服务器端将与 API 对话并获取访问令牌、刷新令牌等。但我不知道之后将访问令牌放在哪里,以便它被每当我使用 Authorize 属性时,都会通过自省 Blazor。 I could just return the tokens and maybe write some javascript that saves it somewhere and adds it to any subsequent http requests, but that does not feel like a Blazor Server solution?我可以只返回令牌,也许写一些 javascript 将其保存在某处并将其添加到任何后续的 http 请求中,但这不像 Blazor 服务器解决方案?

You can store the Access Token in the local storage, and retrieve its value whenever you want to use it.您可以将访问令牌存储在本地存储中,并在您想使用它时检索其值。 Yes, it is Blazor Server solution.是的,它是 Blazor 服务器解决方案。 That is how you should do it.这就是你应该这样做的方式。

When I played around with the Identity support in Blazor Server a cookie with the name ".AspNetCore.Identity.Application" was always created after a successful login.当我在 Blazor 服务器中使用身份支持时,在成功登录后总是会创建一个名为“.AspNetCore.Identity.Application”的 cookie。

This is true.这是真实的。 Is this a statement or you're asking a question here?这是一个声明还是你在这里问一个问题? Anyhow, I guess this cookie will be automatically removed when its life time ends.无论如何,我猜这个 cookie 会在它的生命周期结束时被自动删除。 But in your case, you'll have to do it manually;但是在您的情况下,您必须手动进行; you'll have to write code that checks whether the access token has expired.您必须编写代码来检查访问令牌是否已过期。 If you do not do so, your app will have issues when you try to access a Wep Api endpoint.如果您不这样做,当您尝试访问 Wep Api 端点时,您的应用程序将出现问题。 There is also the authorization components and objects in Blazor that will wrongly perform if you do not manage the stored access token, as for instance, the AuthorizeView embedded in the LoginDisplay component will show the name of an authenticated user (because the claims you extract from the access token constitutes the data from which the AuthenticationSateProvider creates the AuthenticationState object), but no checking of the validity of the access token is perform. Blazor 中还有授权组件和对象,如果您不管理存储的访问令牌,则会错误执行,例如,嵌入在 LoginDisplay 组件中的 AuthorizeView 将显示经过身份验证的用户的名称(因为您从中提取的声明访问令牌构成 AuthenticationSateProvider 创建 AuthenticationState 对象的数据),但不检查访问令牌的有效性。 But accessing your web api with the current access token will result in an exception as the access token is not valid.但是使用当前访问令牌访问您的 web api 将导致异常,因为访问令牌无效。

I've described above something that should be explained by text of hundreds of pages.我在上面描述了一些应该用数百页的文字来解释的东西。 Hope you're not much confused.希望你不会很困惑。

Here's the best place for you to start your investigation Customizing the AuthenticationStateProvider in Blazor Server App with Jwt Token Authentication 这是您开始调查的最佳地点使用 Jwt 令牌身份验证在 Blazor 服务器应用程序中自定义 AuthenticationStateProvider

Hope this helps...希望这可以帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何正确验证 API 中的 OpenIddict JWT access_token? - How to properly validate OpenIddict JWT access_token in API? 不使用OpenIddict接收刷新令牌 - Do not receive refresh token with OpenIddict OpenIdDict 未从.Net Core API 返回令牌 - OpenIdDict not returning Token from .Net Core API 作为客户端,我如何使用客户端 ID 和机密从身份服务器请求访问令牌,然后使用该令牌访问 API? - As a client how do I request an access token from the Identity Server using client ID & secret then use the token to gain access to the API? 通过使用 OpenIddict 与外部提供者登录来获取令牌 - Get Token by loging in with External Providers using OpenIddict OpenIdDict 3.0 在控制器中接受 Id 令牌而不是访问令牌 - OpenIdDict 3.0 Accepting Id Token instead of Access Token in Controllers 在 OpenIdDict 中使用客户端凭据时,指定的令牌无效是无效的 - The specified token is invalid is Invalid when using Client Credentials in OpenIdDict 使用 OpenIdDict 时不允许使用“offline_access”范围 - The 'offline_access' scope is not allowed when using OpenIdDict 如何在 ASP.NET Core 服务器中存储外部 API 的访问令牌? - How do I store an access token for an external API in an ASP.NET Core server? 使用Openiddict的ASP.NET Core 1.0 OAuth服务器 - ASP.NET Core 1.0 OAuth Server using Openiddict
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM