asp.net-core - Blazor 服务器、使用 OpenIdDict 的 API 和使用内省(introspection) - 如何处理访问 token ?

标签 asp.net-core .net-core blazor blazor-server-side openiddict

我有一个 API,它还托管 OpenIdDict token 端点。该 API 没有任何带有登录表单的网页,而是在收到表单帖子后在响应中返回访问 token 。

我之前有一个旧的 AngularJS 前端,它与 API 通信以获取 token 并将其存储在客户端上。 Angular 负责将 token 添加到服务器的每个请求中。

我现在计划使用 Blazor Server 重建前端。我希望新的 Blazor Server 客户端/前端对 API token 端点使用内省(introspection)。

我的计划是构建一个自定义登录页面,该页面将在服务器端与 API 对话并获取访问 token 、刷新 token 等。但我不知道之后将访问 token 放在哪里,以便每当我使用 Authorize 属性时,Blazor 就会通过内省(introspection)来使用它。我可以只返回 token ,也许编写一些 javascript 将其保存在某处并将其添加到任何后续的 http 请求中,但这感觉不像 Blazor 服务器解决方案?

我的最新发现是, token 可以“在 session 中”存储在服务器上,并在客户端上创建“ session 标识符”cookie?可能完全离开这里...

当我在 Blazor Server 中使用身份支持时,成功登录后始终会创建一个名为“.AspNetCore.Identity.Application”的 cookie。

我一直在考虑的另一个不太理想的解决方案或解决方法是将 API 的 OpenIdDict 设置代码复制到 Blazor Server 项目并将它们指向同一个数据库。

如有任何帮助,我们将不胜感激!

最佳答案

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. 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?

您可以将访问 token 存储在本地存储中,并在需要时检索其值。是的,它是 Blazor Server 解决方案。你应该这样做。

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.

这是真的。这是一个陈述还是您在这里提出问题? 不管怎样,我想这个cookie在它的生命周期结束时会被自动删除。但就您而言,您必须手动执行此操作;您必须编写代码来检查访问 token 是否已过期。如果您不这样做,当您尝试访问 Wep Api 端点时,您的应用程序将会出现问题。如果您不管理存储的访问 token ,Blazor 中还有一些授权组件和对象将错误执行,例如,LoginDisplay 组件中嵌入的 AuthorizeView 将显示经过身份验证的用户的名称(因为您从其中提取的声明)访问 token 构成 AuthenticationSateProvider 创建 AuthenticationState 对象的数据),但不检查访问 token 的有效性。但 使用当前访问 token 访问您的 Web api 将导致异常,因为访问 token 无效。

上面我已经描述了一些需要数百页文字来解释的事情。希望您不会感到困惑。

Here's the best place for you to start your investigation Customizing the AuthenticationStateProvider in Blazor Server App with Jwt Token Authentication

希望这有帮助...

关于asp.net-core - Blazor 服务器、使用 OpenIdDict 的 API 和使用内省(introspection) - 如何处理访问 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62574045/

相关文章:

c# - 如何降低 dockerized .Net Core 应用程序的大小?

c# - 将数据传递给 startup.cs

asp.net-core - 将 Blazor 组件放置在用户内容内

c# - .NET Core Healthchecks UI 未显示

c# - .Net 核心托管服务保证完成

c# - 所有 Blazor 页面的公共(public)属性?

c# - 使用 .NET 5.0 中的 Blazor WebAssembly 应用程序渲染美人鱼图

javascript - react 自定义组件(数据表)

c# - 当我从 ASP.NET Core 1.x 升级到 2.0 时,Identity CookieOptions 的替代品是什么?

.net - 从 Linux 上的 .net 核心应用程序使用 ntlm 身份验证访问 MS SQL Server