2017-06-05 25 views
0

我想設置我們的IdentityServer解決方案來接受自定義格蘭特驗證。我們的API項目可以通過UI訪問,一個使用密碼認證(正在工作),另一個使用第三方認證。不支持的授權類型與IdentityServer 3與CustomGrantValidator

在我們的API我已經設置了IdentityServer像這樣:

Startup.cs

public void Configuration(IAppBuilder app) 
    { 
     var factory = new IdentityServerServiceFactory() 
      .UseInMemoryClients(Clients.Get()) 
      .UseInMemoryScopes(Scopes.Get()); 

     var userService = new IdentityUserService(); 
     factory.UserService = new Registration<IUserService>(resolver => userService); 
     factory.CustomGrantValidators.Add(
      new Registration<ICustomGrantValidator, MyGrantValidator>()); 

     var options = new IdentityServerOptions 
     { 
      SiteName = "My App Name", 
      SigningCertificate = Certificate.Get(), 
      Factory = factory 
     }; 

     app.Map("/identity", identityServerApp => 
     { 
      identityServerApp.UseIdentityServer(options); 
     }); 
} 

MyGrantValidator.cs:

public class MyGrantValidator : ICustomGrantValidator 
{ 
    public async Task<CustomGrantValidationResult> ValidateAsync(ValidatedTokenRequest request) 
    { 
     // For now I just want a basic response. More logic will come later. 
     var authResult = new AuthenticateResult(
      subject: "1234", // user.AccountId.ToString(), 
      name: "bob" //context.UserName 
     ); 
     var grantResult = new CustomGrantValidationResult 
     { 
      IsError = authResult.IsError, 
      Error = authResult.ErrorMessage, 
      ErrorDescription = authResult.ErrorMessage, 
      Principal = authResult.User 
     }; 
     return await Task.FromResult(grantResult); 
    } 

    public string GrantType => "myGrantType"; 
} 

在我的UI,我安裝一個客戶端像這個:

 var owinContext = HttpContext.GetOwinContext(); 
     var token = owinContext.Authentication.User.FindFirst(c => c.Type == "myToken")?.Value; 
     var tokenId = owinContext.Authentication.User.FindFirst(c => c.Type == ClaimTypes.Sid)?.Value; 

     var client = new TokenClient(
      ConfigurationManager.AppSettings["IdentityServerBaseUrl"] + "/connect/token", 
      "MyUser", 
      ConfigurationManager.AppSettings["MyClientSecret"], 
      AuthenticationStyle.Custom 
     );    

     var tokenResponse = client.RequestCustomGrantAsync(
      "myGrantType", 
      "read write", 
      new Dictionary<string, string> 
      { 
       { "token", token }, 
       { "tokenId", tokenId } 
      } 
     ).Result; 

     return Redirect(returnUrl); 

當請求被觸發時,我得到:unsupported_grant_type

我錯過了什麼?

回答

1

您正在使用名爲「MyUser」的客戶端(客戶端的名稱很奇怪,但確定)。該客戶是否被註冊爲授權類型設置爲「自定義」的內存中客戶之一?