1. 程式人生 > >identityServer4 中的概念(Scope,claim)

identityServer4 中的概念(Scope,claim)

在IdentityServer中好多地方出現這幾個詞,這單詞的解釋也有好多大神解釋過:

chaim:

 

ASP.NET Core 之 Identity 入門(一),這個是asp.net identity中chaim的解釋,雖然不是identityServer,但解釋相當精準。

IdentityServer4實戰 - 基於角色的許可權控制及Claim詳解 ,這是是Claim 在identityServer中的解釋。

首先檢視identityServer中chaim出現的地方:

Clientclaims、Identityclaims、apiclaims和APIScopeclaims,說明claim與這些東東都有關係,按照上面第一篇部落格的說明,這個claim是 “證件單元

”,這個apiclaims就解釋為api的需要的證件單元,也不是太合適;按照第二篇部落格說的claim是“使用者資訊單元”,只有解釋使用者的時候可以這麼說。

檢視identityServer原始碼,claim包含type和value兩個property,在identity、apiresource和apiscope中都有(UserClaims)這個屬性,官方解釋:List of associated user claim types that should be included in the access token.就是需要在access token中應該包含的一系列相關claim 型別,如果access token中含有這個claim可以訪問這個resource,另外APIScopeclaims中的claim也會附加到這個api資源上。

Scopes:

英文翻譯:範圍

Scope出現在兩個地方:clientScope和apiscope。

Scope 在ids3中的解釋:IdentityServer.Core.Models.Scope 類是對 OpenID Connect 或 OAuth2 scope 的建模。這個解釋很合理;

在token中scope是一個claim,這個很重要:scope也是一個claim;

apiscope,一個api至少有一個scope,下面的兩個定義是一個意思:

 1 new ApiResource("api1", "Some API 1")
 2 //與下面的意思相同 3
4 new ApiResource 5 { 6 Name = "api1", 7 DisplayName = "Some API 1", 8 9 Scopes = 10 { 11 new Scope() 12 { 13 Name = "api1", 14 DisplayName = "Some API 1" 15 } 16 } 17 }

一個api也可以有多個scope,例如:

 1 new ApiResource
 2 {
 3     Name = "api2",
 4 
 5     Scopes =
 6     {
 7         new Scope()
 8         {
 9             Name = "api2.full_access",
10             DisplayName = "Full access to API 2"
11         },
12         new Scope
13         {
14             Name = "api2.read_only",
15             DisplayName = "Read only access to API 2"
16         }
17     }
18 }