IdentityServer3中客戶端保護了受權資源,不難看出在IdentityServer3中,有這樣一個設置web
AllowedScopes = new List<string> { "clientservices" }
經過上面的客戶端,拿到了四個受權範圍,好比我有一個WebApi的資源受權服務站點,以下面的設置算法
app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions { Authority = LYM.Unity.AppSetting.AppSettingsHelper.GetString("Authority"), ValidationMode = ValidationMode.ValidationEndpoint, RequiredScopes = new[] { "clientservices" } });
比較能夠看出 經過客戶端拿到的受權範圍是能夠訪問Webapi的資源服務,經過其餘不包含clientservices的客戶端拿到的令牌是不具備這個webapi資源受權權限,因此客戶端中的Scope設置更好的把資源分類保護起來了,而咱們要作的就是對資源分類,對不一樣的客戶端設置具體訪問的資源分類,具體業務看具體需求,好比同一個客戶端也能夠同時具備多個資源訪問權限api
訪問令牌是根據客戶端設置的Scope範圍,頒發給客戶端的一個資源訪問的字符串令牌,這個令牌對客戶端是保密不透明的,客戶端經過資源擁有者提供的受權及受權範圍去訪問具體的受權資源,如現有不少微信、QQ 第三方登陸同樣的,咱們用戶做爲一個資源擁有者,而咱們要訪問網站做爲一個客戶端(這個客戶端QQ、微信授予的Client),咱們的基本資料等做爲咱們的資源,當咱們用微信訪問的時候會轉到微信登陸的一個客戶端(提示是具體客戶端名稱-如某某網站),用戶肆意受權後,經過此客戶訪問資源權限並攜帶微信提供的受權碼回到 網站上,咱們在用這個受權碼令牌去驗證。微信
令牌用於檢索受權信息的標識,經過自定義的一些可驗證的方式、算法檢索受權信息,可包含受權信息、證書信息,若是不符合相關要求,急須要客戶端提供一個有效的令牌。app
IdentityServer3中提供了相關的接口注入方式async
factory.CustomTokenValidator = new Registration<ICustomTokenValidator, CustomTokenValidator>();
public class CustomTokenValidator : ICustomTokenValidator { public async Task<TokenValidationResult> ValidateAccessTokenAsync(TokenValidationResult result) { return await Task.FromResult(result); } public async Task<TokenValidationResult> ValidateIdentityTokenAsync(TokenValidationResult result) { return await Task.FromResult(result); } }
接口實現中 ,在這個接口中看下 TokenValidationResult 這個類ide
public class TokenValidationResult : ValidationResult { public TokenValidationResult(); // // 摘要: // Gets or sets the claims. public IEnumerable<Claim> Claims { get; set; } // // 摘要: // Gets or sets the client. public Client Client { get; set; } // // 摘要: // Gets or sets the JWT. public string Jwt { get; set; } // // 摘要: // Gets or sets the reference token. public Token ReferenceToken { get; set; } // // 摘要: // Gets or sets the reference token identifier. public string ReferenceTokenId { get; set; } }
ValidateAccessTokenAsync這個驗證方法 是驗證Token, 這裏包含了Client信息,Claim信息 還有 Jwt (Json Web Token)Token的信息 以及 ReferenceToken 引用相關的信息網站
ValidateIdentityTokenAsync 登出的時候會調用這個方法,在能夠作處理好比清除相關Jwt ReferenceTokenui