ASP.NET Core學習之七 認證授權

簡介

一直以來都是使用identity來做驗證,因爲ABP已經集成好的,但到了.NET CORE 3.0後一直想去改變引用.net 版本的identity問題,使用的是.NET FRAMWORK 4.6,本文就是爲了脫離identity而寫的

問題解析

使用ABP的時候,登錄的時候,使用的是identity的UserManager.CreateIdentityAsyn來創建,ABP的AbpSession調用的是從這裏拿到userId的,所以想要擴展AbpSession,需要由自己定義才能實現

登陸

使用官方CookieAuthentication身份驗證Web程序

startup

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    services.AddAuthentication(options =>
    {
        options.DefaultScheme = "Cookies";
    }).AddCookie();
}


public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
    app.UseAuthentication(); 
}

ClaimsIdentity

 var claimsIdentity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
            claimsIdentity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()));
            claimsIdentity.AddClaim(new Claim(AbpClaimTypes.UserId, user.Id.ToString()));
            claimsIdentity.AddClaim(new Claim(AbpClaimTypes.UserName, user.UserName.ToString()));
            if (tenant != null)
            {
                claimsIdentity.AddClaim(new Claim(AbpClaimTypes.TenantId, tenant.Id.ToString()));
            }
            

IHttpContextAccessor

    //校驗、登記成功後
    await _contextAccessor.HttpContext.SignInAsync(new ClaimsPrincipal(result.Identity));

內容不詳,僅供參考

參考

認證相關類簡要說明一

使用ClaimsIdentity實現登錄授

基於IHttpContextAccessor實現系統級別身份標識

Authentication認證

【ASP.NET Core】運行原理(3):認證

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