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):认证

posted @ 2020-08-09 23:29  心存善念  阅读(412)  评论(0编辑  收藏  举报