這篇英文博文是 Andrew Lock 寫的 Introduction to Authentication with ASP.NET Core 。cookie
如下是簡單的閱讀筆記:ide
-----------------------------------spa
ASP.NET Core 的驗證模型是 claims-based authentication 。Claim 是對被驗證主體特徵的一種表述,好比:登陸用戶名是...,email是...,用戶Id是...,其中的「登陸用戶名」,「email」,「用戶Id」就是ClaimType。.net
You can think of claims as being a statement about...That statement consists of a name and a value.code
對應現實中的事物,好比駕照,駕照中的「身份證號碼:xxx」是一個claim,「姓名:xxx」是另外一個claim。blog
一組claims構成了一個identity,具備這些claims的identity就是 ClaimsIdentity ,駕照就是一種ClaimsIdentity,能夠把ClaimsIdentity理解爲「證件」,駕照是一種證件,護照也是一種證件。ip
ClaimsIdentity的持有者就是 ClaimsPrincipal ,一個ClaimsPrincipal能夠持有多個ClaimsIdentity,就好比一我的既持有駕照,又持有護照。ci
------------------------------------get
理解了Claim, ClaimsIdentity, ClaimsPrincipal這三個概念,就能理解生成登陸Cookie爲何要用下面的代碼?it
var claimsIdentity = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, loginName) }, "Basic"); var claimsPrincipal = new ClaimsPrincipal(claimsIdentity); await context.Authentication.SignInAsync(_cookieAuthOptions.AuthenticationScheme, claimsPrincipal);
要用Cookie表明一個經過驗證的主體,必須包含Claim, ClaimsIdentity, ClaimsPrincipal這三個信息,以一個持有合法駕照的人作比方,ClaimsPrincipal就是持有證件的人,ClaimsIdentity就是證件,"Basic"就是證件類型(這裏假設是駕照),Claim就是駕照中的信息。