Asp.net Core, 基於 claims 實現許可權驗證 - 引導篇

來源:互聯網
上載者:User

標籤:html   分享   ddd   cat   ddc   閱讀   使用者   asp.net   media   

什麼是Claims?這個直接閱讀其他大神些的文章吧,解釋得更好。相關文章閱讀:http://www.cnblogs.com/JustRun1983/p/4708176.htmlhttp://www.cnblogs.com/jesse2013/p/aspnet-identity-claims-based-authentication-and-owin.htmlhttp://www.cnblogs.com/savorboard/p/aspnetcore-identity.html 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組成了一個identityClaimsPrincipal: 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,完成許可權驗證      

Asp.net Core, 基於 claims 實現許可權驗證 - 引導篇

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.