Blazor Server完美实现Cookie Authorization and Authentication

Blazor server-side application用Microsoft.AspNetCore.Identity.EntityFrameworkCore实现Authorization 和 Authentication 完整教程。

本方案只适用于Blazor Server-Size Application

完整项目源代码,参考: https://github.com/neozhu/CleanArchitectureWithBlazorServer

需要引用的类库如下:

    <PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="7.0.0" />     <PackageReference Include="Microsoft.AspNetCore.Authentication.MicrosoftAccount" Version="7.0.0" />     <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="7.0.0" />     <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="7.0.0" />     <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.0">       <PrivateAssets>all</PrivateAssets>       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>     </PackageReference>     <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.0" />     <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0" />     <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.0">       <PrivateAssets>all</PrivateAssets>       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>     </PackageReference>     <PackageReference Include="Duende.IdentityServer" Version="6.2.0" />     <PackageReference Include="Duende.IdentityServer.AspNetIdentity" Version="6.2.0" />     <PackageReference Include="Duende.IdentityServer.EntityFramework" Version="6.2.0" />     <PackageReference Include="Duende.IdentityServer.EntityFramework.Storage" Version="6.2.0" />     <PackageReference Include="Duende.IdentityServer.Storage" Version="6.2.0" /> 

  

这里的实现方式和Asp.net core 3.0,5.0,6.0, 7.0 几乎一样的配置,但又也有一些特殊之处。下面我分享一下的代码。

从上面引用的类库发现我并使用的是Microsoft.AspNetCore.Identity.EntityFrameworkCore + Duende.IdentityServer 都已经升级到最新版本。

配置 Microsoft.AspNetCore.Identity.EntityFrameworkCore 

用于生成需要后台表

Blazor Server完美实现Cookie  Authorization and Authentication

 

 

 这里和微软官方的文档略有不同我使用的AddIdentity方法。

添加 Authorization and Authentication 配置

Blazor Server完美实现Cookie  Authorization and Authentication

 

 

 这类servicescollection配置和asp.net core cookie认证是一直,只是这里不需要配置Login,Logout路径

开发一个登录Blazor Component(Page)

Blazor Server完美实现Cookie  Authorization and Authentication

Blazor Server完美实现Cookie  Authorization and Authentication

 

 重点这里需要生成一个Token,而不是直接传用户名+密码,因为安全 不能明文传输密码。这里我们需要调用auth/login?token=.... 实现登录

AuthController 用户登录并获取授权

Blazor Server完美实现Cookie  Authorization and Authentication

 

 这里的写法和asp.net core登录一样都使用SignInManager<ApplicationUser> 登录成功后和asp.net core应用一样保存于账号相关的所有授权比如Roles和Claims 

如何需要自定义添加自定义的内容比如下面的TenantId TenantName ,ApplicationClaimsIdentityFactory就是用于添加需要内容。

Blazor Server完美实现Cookie  Authorization and Authentication

 

 获取当前登录的账号信息

Blazor Server完美实现Cookie  Authorization and Authentication

 

 之前Blazor Server-Side application 是不支持 IHttpContextAccessor获取账号信息,现在竟然可以了。

Blazor server Component调用UserManager<ApplicationUser>需要注意的地方

Blazor Server完美实现Cookie  Authorization and Authentication

 

Component需要继承 添加 @inherits OwningComponentBase

Blazor Server完美实现Cookie  Authorization and Authentication

 

 需要通过ScopedServices.GetRequiredService<UserManager<ApplicationUser>>(); 创建才安全

 

解决 Asp.net core bad request headers to long · Issue

这个问题的原因是浏览器对request header 长度有限制,当我们的用户关联了太多的权限permissions set, 系统默认把这些信息全部加密后存在 Cookie Name .AspNetCore.Identity.Application这里,你会发现非常大。

我的做法就是要把这些信息保存到内存里当然也可以保存到数据库中,选中保存内存更简单,但是如果服务器重启或是资源回收,客户端需要重新登录,并且会占用服务器内容。

第一步:创建一个MemoryTicketStore用于存放Identity信息

Blazor Server完美实现Cookie  Authorization and Authentication

 

 第二部 修改配置把认证授权信息从cookie转存到我们指定的内存里

Blazor Server完美实现Cookie  Authorization and Authentication

 

 Blazor Server完美实现Cookie  Authorization and Authentication

 

 这样问题就解决了

 

 

 

 

希望对学习Blazor的同学有帮助。

 

发表评论

评论已关闭。

相关文章