2017-10-12 169 views
0

我有一個基本的IdentityServer4令牌服務器,一個Api和一個基於identityserver4文檔教程使用client_credentials的測試客戶端應用程序設置。IdentityServer4 IExtensionGrantValidator

我們有一個預先構建的客戶端應用程序,用戶使用他們現有的憑據登錄,這些憑據不與IdentityServer4綁定。客戶端應用程序將使用client_credentials工作流調用Api,因爲我不想爲可能需要訪問Api的每個客戶端應用程序創建多個用戶。

在IdentityServer4上使用上述設置我已使用client_Credentials工作流程正常工作。我面臨的問題是雖然我不需要個人用戶進行身份驗證,但我仍然想通過user_id知道他們是誰。我可以簡單地將& user_id = 9999添加到令牌請求中,但我找不到在請求發出時從令牌服務器檢索此信息的方法。經過一些研究後,我遇到了IExtensionGrantValidator,它允許我添加一個cstom授權類型並攔截請求並執行一些自定義處理。問題是,即使它看起來好像我正確設置它,我仍然得到invalid_grant錯誤。

下面是代碼:

public class CustomGrantValidator : IExtensionGrantValidator 
{ 
    public string GrantType => "custom_credentials"; 

    public Task ValidateAsync(ExtensionGrantValidationContext context) 
    { 
     return Task.FromResult(context.Result); 
    } 
} 

在新的客戶端塊:

AllowedGrantTypes = 
{ 
    GrantType.ClientCredentials, 
    "custom_credentials" 
}, 

在啓動

.AddExtensionGrantValidator<CustomGrantValidator>(); 

我是新來IdentityServer4和.net核心,所以我我確信我做錯了一件事,或者不瞭解這裏的基本機制。

回答

0

我發現這是因爲我遇到了同樣的錯誤,但我相信你的驗證器是無用的,因爲它什麼都不做。您需要在上下文中設置結果,如下所示:

 var claims = new List<Claim>(); 

     claims.Add(new Claim("sub", userToken)); 
     claims.Add(new Claim("role", "admin")); 

     context.Result = new GrantValidationResult(userToken, "delegation", claims: claims); 

我看到的每個示例都通過添加此值來設置結果。