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 

用於生成需要後臺表

 

 

 這裏和微軟官方的文檔略有不同我使用的AddIdentity方法。

添加 Authorization and Authentication 配置

 

 

 這類servicescollection配置和asp.net core cookie認證是一直,只是這裏不需要配置Login,Logout路徑

開發一個登錄Blazor Component(Page)

 

 重點這裏需要生成一個Token,而不是直接傳用戶名+密碼,因爲安全 不能明文傳輸密碼。這裏我們需要調用auth/login?token=.... 實現登錄

AuthController 用戶登錄並獲取授權

 

 這裏的寫法和asp.net core登錄一樣都使用SignInManager<ApplicationUser> 登錄成功後和asp.net core應用一樣保存於賬號相關的所有授權比如Roles和Claims 

如何需要自定義添加自定義的內容比如下面的TenantId TenantName ,ApplicationClaimsIdentityFactory就是用於添加需要內容。

 

 獲取當前登錄的賬號信息

 

 之前Blazor Server-Side application 是不支持 IHttpContextAccessor獲取賬號信息,現在竟然可以了。

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

 

Component需要繼承 添加 @inherits OwningComponentBase

 

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

 

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

這個問題的原因是瀏覽器對request header 長度有限制,當我們的用戶關聯了太多的權限permissions set, 系統默認把這些信息全部加密後存在 Cookie Name .AspNetCore.Identity.Application這裏,你會發現非常大。

我的做法就是要把這些信息保存到內存裏當然也可以保存到數據庫中,選中保存內存更簡單,但是如果服務器重啓或是資源回收,客戶端需要重新登錄,並且會佔用服務器內容。

第一步:創建一個MemoryTicketStore用於存放Identity信息

 

 第二部 修改配置把認證授權信息從cookie轉存到我們指定的內存裏

 

 

 

 這樣問題就解決了

 

 

 

 

希望對學習Blazor的同學有幫助。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章