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,完成權限驗證
 
 
 
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM