Asp.net Core, 基于 claims 实现权限验证 - 引导篇

什么是Claims?
这个直接阅读其他大神些的文章吧,解释得更好。
相关文章阅读:
 
claims 姑且叫做声明,可以理解为和用户相关的一条一条信息的描述,可以是用户的身份信息(Name,Email,ID)也可以是用户的角色,甚至是一些自定义的Claims
 
关于Claims 和 Claims - base, 博客园的讲述已经很多了, 可以查阅相关文章。这里只是介绍如何给予Asp.net Identity Claims 来实现业务系统的权限验证。
 
 
在使用Identity做为系统的登陆和权限验证时,常常会用到角色,其实角色也是一种Claims, 而且角色的验证也是ClaimsBase的。
 
新建一个asp.net core Web Application 项目,修改验证类型为: Individual User Accounts。使用默认的项目模板
 

 

 
默认的项目模板已经为我们集成好了基于Asp.net identity的 登陆验证功能。
运行项目,注册用户,登陆。
为了验证角色也是基于Claims的,我们并没有为用户设置角色。
现在想在访问Action时,添加上基于角色的验证。

 

显然是 无法访问 home/index的。

 

原因是因为我们并没有为用户添加“MyRole”这个角色。
 
假如我们并不想为用户添加一个"MyRole"的角色,而是想在用户登录时,为用户添加一个 ClaimType 为 Role的 Claims ,看看是否能通过验证。
OK, 来试试看。
 
重要对象:Claims, ClaimsIdentity ClaimsPrincipal
 
可以这样理解;
Claims:
ClaimsIdentity: 可以这样理解,一组Cliams 就构成了一个Identity,比如身份证:姓名,性别,身份证号,等一系列Claims组成了一个identity
ClaimsPrincipal: ClaimsIdentity的持有者。一个ClaimsPrincipal可以持有多个ClaimsIdentity。
了解了这些概念后,我们就知道如果要给用户添加新的/自定义的Claims该往哪加了。
 
而 asp.net Identity在登陆时,会通过 UserClaimsPrincipalFactory 的 CreateAsync,来创建 ClaimsPrincipal。
那么我们需要做的,就是继承UserClaimsPrincipalFactory, 自定义一个AppClaimsPrincipalFactory
并重写 CreateAsync方法
 
 public class AppClaimsPrincipalFactory:UserClaimsPrincipalFactory<ApplicationUser,IdentityRole>
    {
        public AppClaimsPrincipalFactory(UserManager<ApplicationUser> userManager, 
            RoleManager<IdentityRole> roleManager, 
            IOptions<IdentityOptions> optionsAccessor) : base(userManager, roleManager, optionsAccessor)
        {
        }

        public async override Task<ClaimsPrincipal> CreateAsync(ApplicationUser user)
        {
            var principal = await base.CreateAsync(user);
            ((ClaimsIdentity)principal.Identity).AddClaims(new[] {
            new Claim(ClaimTypes.Role, "MyRole")
        });

            return principal;
        }
    }

 

 
在CreateAsync 方法中,先调用base.CreateAsync()方法,获取一个ClaimsPrinciapl对象,然后再往ClaimsPrincipal。Identity中 添加我们想要的自定 Claims。
 
如图, 我们加入 new Claim(ClaimTypes.Role, "MyRole")
 
然后在Start Up 方法中,将重写的AppClaimsPrincipalFactory 注入到服务中
public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddApplicationInsightsTelemetry(Configuration);

            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

            services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, AppClaimsPrincipalFactory>();


            services.AddMvc();

            // Add application services.
            services.AddTransient<IEmailSender, AuthMessageSender>();
            services.AddTransient<ISmsSender, AuthMessageSender>();
        }

 

启动,运行,home/index页面可以正常访问了。
可以得知,Role 也是基于 Claims base的。
既然自定义的Claims 也能完成权限验证,那么在业务系统中,也通过各种Claims来完成各种权限验证。类似于,登陆系统后,系统给你发放各种证件,然后就可以通过你所拥有的证件,在系统中通行了。
 
接下来,我们根据业务需要,来定制各种Claims,完成权限验证
 
 
 
 
 
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章