Authentication and Authorisation

原文鏈接:https://andrewlock.net/introduction-to-authentication-with-asp-net-core/

who you are   ===  》what you are allowed to do

https://andrewlock.net/introduction-to-authentication-with-asp-net-core/

The difference between Authentication and Authorisation

First of all, we should clarify the difference between these two dependent facets of security. The simple answer is that Authentication is the process of determining who you are, while Authorisation revolves around what you are allowed to do, i.e. permissions. Obviously before you can determine what a user is allowed to do, you need to know who they are, so when authorisation is required, you must also first authenticate the user in some way.

 

https://www.cnblogs.com/dudu/p/6367303.html

 

 駕照中的“身份證號碼:xxx”是一個claim,“姓名:xxx”是另一個claim。

一組claims構成了一個identity,具有這些claims的identity就是 ClaimsIdentity ,駕照就是一種ClaimsIdentity,可以把ClaimsIdentity理解爲“證件”,駕照是一種證件,護照也是一種證件。

ClaimsIdentity的持有者就是 ClaimsPrincipal ,一個ClaimsPrincipal可以持有多個ClaimsIdentity,就比如一個人既持有駕照,又持有護照。

 

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就是駕照中的信息。 

 

 

 

 

 

https://www.cnblogs.com/savorboard/p/aspnetcore-identity.html

public class Claim { public string ClaimType { get; set; } public string ClaimValue { get; set; } }、

# 1: Claims

ClaimType 就是Key,ClaimValue就代表一個Value。這樣的話,剛好可以存儲一個鍵值對。這時候姓名:奧巴馬是不是可以存進去了

# 2: ClaimsIdentity

在有了“證件單元”之後,我們就用它可以製造一張身份證了,那麼應該怎麼樣製造呢?有些同學可能已經想到了,對,就是新建一個對象,然後在構造函數裏面把身份證單元傳輸進去,然後就得到一張身份證了。我們給這張身份證取一個英文名字叫 “ClaimsIdentity”,這個名字看起來還蠻符合的,既有 Claims 表示其組成部分,又有表示其用途的 Identity(身份),很滿意的一個名字。

 

# 3: ClaimsPrincipal

有了身份證,我們就能證明我就是我了,有些時候一個人有很多張身份證,你猜這個人是幹嘛的? 對,不是黃牛就是詐騙犯。

但是,有些時候一個人還有其他很多種身份,你猜這個人是幹嘛的?這就很正常了對不對,比如你可以同時是一名教師,母親,商人。如果你想證明你同時有這幾種身份的時候,你可能需要出示教師證,你孩子的出生證,法人代表的營業執照證。

在程序中,一個身份證不僅僅代表你這個人了,而是代表一個身份,是證明你自己的主要身份哦。如果一個人還有其他很多種身份,這個時候就需要有一個東西(載體)來攜帶着這些證件了對吧?OK,我們給需要攜帶證件的這個對象取一個貼切點的名字,叫“證件當事人(ClaimsPrincipal)”吧。

首先我們在app這邊有一些證件單元,然後調用ClaimsIdentity把證件單元初始化爲一個身份證,然後再把身份證交給證件當事人由其保管

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章