1. 程式人生 > >asp.net core系列 52 Identity 其它關註點

asp.net core系列 52 Identity 其它關註點

sign 文檔 code login authent server 瀏覽器 rman blank

原文:asp.net core系列 52 Identity 其它關註點

一.登錄分析

  在使用identity身份驗證登錄時,在login中調用的方法是:

  var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: true);

  跟蹤查看源碼,源碼下載https://github.com/aspnet/AspNetCore/releases 這裏有core源碼的不同版本,在vs 2017下只能加載2.2及以下的版本。

  下面是登錄的大概步驟:

  (1) 檢查用戶名是否存在(UserManager.cs在Microsoft.AspNetCore.Identity.core源碼中)

    var user = await UserManager.FindByNameAsync(userName);

  (2) UserManager類來檢查用戶名和密碼是否存在

    UserManager.CheckPasswordAsync(user, password)

  (3) 登錄,isPersistent是指瀏覽器關閉後登錄cookie是否應該保持,如果是true則永久保存cookie,如果為false則使用services.ConfigureApplicationCookie中options.ExpireTimeSpan 來重寫。SignInOrTwoFactorAsync(user, isPersistent)方法最終調用SignInAsync

進行登錄。

       public virtual async Task SignInAsync(TUser user, AuthenticationProperties authenticationProperties, string authenticationMethod = null)
        {
            var userPrincipal = await CreateUserPrincipalAsync(user);
            // Review: should we guard against CreateUserPrincipal returning null?
if (authenticationMethod != null) { userPrincipal.Identities.First().AddClaim(new Claim(ClaimTypes.AuthenticationMethod, authenticationMethod)); } await Context.SignInAsync(IdentityConstants.ApplicationScheme, userPrincipal, authenticationProperties ?? new AuthenticationProperties()); }

    AuthenticationProperties:用來存儲身份認證會話

    IdentityConstants:是配置Identity系統使用的cookie中間件的所有選項, ApplicationScheme屬性是指:該方案運用於Identity應用程序的cookies(默認方案)。如下所示:

          private static readonly string CookiePrefix = "Identity";
          public static readonly string ApplicationScheme = CookiePrefix + ".Application"

    登錄涉及到三個類ClaimsPrincipal(聲明當事人)、ClaimsIdentity(聲明標識)、Claim(聲明)。

    Claim:是名稱值對,比如名稱ClaimType:身份證, 值ClaimValue:18位號碼。

    ClaimsIdentity:一組Cliams 就構成了一個Identity標識。

    ClaimsPrincipal:當事人可以持有多個ClaimsIdentity標識。

    最後SignInAsync 創建一個加密的 cookie,並將其添加到當前響應。

二.註銷

  若要註銷(退出登錄)當前用戶,然後刪除其 cookie,需要調用SignOutAsync 。

    await HttpContext.SignOutAsync();    

三. Identity表管理

  3.1可以使用UserManager類和RoleManager類來管理Identity表,可以參考"通過授權創建web應用",下面是聲明的新增方法

    //添加用戶聲明 Microsoft.AspNetCore.Identity.UserManager<TUser>
    public virtual Task<IdentityResult> AddClaimAsync(TUser user, Claim claim)

    //添加角色聲明 Microsoft.AspNetCore.Identity.RoleManager<TRole>
    public virtual async Task<IdentityResult> AddClaimAsync(TRole role, Claim claim)

  3.2 在UserManager下,會發現很多方法,都是傳入ClaimsPrincipal參數,如下所示:

    //獲取用戶ID
    GetUserId(ClaimsPrincipal principal)
    //獲取用戶
    Task<TUser> GetUserAsync(ClaimsPrincipal principal)

    可以通過如下來轉換成ClaimsPrincipal:

    ClaimsPrincipal principal = HttpContext.Current.User as ClaimsPrincipal;

  3.3 Claim聲明類

    聲明值Value:對於簡單的聲明值可以使用字符串存儲,更復雜的值類型,建議使用標準的 XML (或json)架構類型,在應用程序端序列化和反序列化。

    聲明類型Type:標識值的類型信息。

    其它屬性, 如定義頒發聲明等,參考官方文檔。

    

四.不使用identity系統進行身份認證

  如果開發想自定義用戶表,角色表等,完全拋棄identity系統,實現參考"使用 cookie 而無需 ASP.NET Core 標識的身份驗證“

五. Identity擴展

  (1) 如果想使用不同數據訪問方法,不使用默認的EF Core。

  (2) 如果不想使用 SQL Server存儲用戶信息,想使用其它數據存儲。

  (3) 對Identity表想使用不同的結構。

  實現參考"ASP.NET Core標識的自定義的存儲提供程序”

    

六. Identity配置

  對於 ASP.NET Core Identity設置,例如密碼策略、 鎖定和 cookie 配置使用默認值等。參考文檔 "配置標識"

    

七. 帳戶確認和 ASP.NET Core 中的密碼恢復

  https://docs.microsoft.com/zh-cn/aspnet/core/security/authentication/accconfirm?view=aspnetcore-2.2&tabs=visual-studio  

    

asp.net core系列 52 Identity 其它關註點