簡介
一直以來都是使用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));
內容不詳,僅供參考