IdentityServer3:OWIN Katana中间件抛出“invalid_client”错误,因为它无法获得令牌

问题描述:

我们使用IdentityServer3作为身份提供者和OWIN Katana中间件来执行基于OpenId Connect的握手。身份验证正常工作,因为我们被重定向到身份服务器并返回到原始网站。但是当我尝试检索令牌并获取“OpenIdConnectAuthenticationNotifications”中的声明时,会出现invalid_client问题。IdentityServer3:OWIN Katana中间件抛出“invalid_client”错误,因为它无法获得令牌

请检查下面的代码(启动类)和附加的屏幕截图。

public sealed class Startup 
{ 
    public void Configuration(IAppBuilder app) 
    { 
     string ClientUri = @"https://client.local"; 
     string IdServBaseUri = @"https://idm.website.com/core";l 
     string TokenEndpoint = @"https://idm.website.com/core/connect/token"; 
     string UserInfoEndpoint = @"https://idm.website.com/core/connect/userinfo"; 
     string ClientId = @"WebPortalDemo"; 
     string ClientSecret = @"aG90apW2+DbX1wVnwwLD+eu17g3vPRIg7p1OnzT14TE="; 

     //AntiForgeryConfig.UniqueClaimTypeIdentifier = "sub"; 
     JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>(); 

     app.UseCookieAuthentication(new CookieAuthenticationOptions 
     { 
      AuthenticationType = "Cookies" 
     }); 

     app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions 
     { 
      ClientId = ClientId, 
      Authority = IdServBaseUri, 
      RedirectUri = ClientUri, 
      PostLogoutRedirectUri = ClientUri, 
      ResponseType = "code id_token token", 
      Scope = "openid profile roles", 
      TokenValidationParameters = new TokenValidationParameters 
      { 
       NameClaimType = "name", 
       RoleClaimType = "role" 
      }, 
      SignInAsAuthenticationType = "Cookies", 

      Notifications = new OpenIdConnectAuthenticationNotifications 
      { 
       AuthorizationCodeReceived = async n => 
       { 
        // use the code to get the access and refresh token 
        var tokenClient = new TokenClient(
         TokenEndpoint, 
         ClientId, 
         ClientSecret); 

        var tokenResponse = await tokenClient.RequestAuthorizationCodeAsync(n.Code, n.RedirectUri); 

        if (tokenResponse.IsError) 
        { 
         throw new Exception(tokenResponse.Error); 
        } 

        // use the access token to retrieve claims from userinfo 
        var userInfoClient = new UserInfoClient(UserInfoEndpoint); 

        var userInfoResponse = await userInfoClient.GetAsync(tokenResponse.AccessToken); 

        // create new identity 
        var id = new ClaimsIdentity(n.AuthenticationTicket.Identity.AuthenticationType); 
        //id.AddClaims(userInfoResponse.GetClaimsIdentity().Claims); 
        id.AddClaims(userInfoResponse.Claims); 

        id.AddClaim(new Claim("access_token", tokenResponse.AccessToken)); 
        id.AddClaim(new Claim("expires_at", DateTime.Now.AddSeconds(tokenResponse.ExpiresIn).ToLocalTime().ToString())); 
        id.AddClaim(new Claim("refresh_token", tokenResponse.RefreshToken)); 
        id.AddClaim(new Claim("id_token", n.ProtocolMessage.IdToken)); 
        id.AddClaim(new Claim("sid", n.AuthenticationTicket.Identity.FindFirst("sid").Value)); 

        n.AuthenticationTicket = new AuthenticationTicket(
         new ClaimsIdentity(id.Claims, n.AuthenticationTicket.Identity.AuthenticationType, "name", "role"), 
         n.AuthenticationTicket.Properties); 
       }, 

       RedirectToIdentityProvider = n => 
       { 
        // if signing out, add the id_token_hint 
        if (n.ProtocolMessage.RequestType == OpenIdConnectRequestType.LogoutRequest) 
        { 
         var idTokenHint = n.OwinContext.Authentication.User.FindFirst("id_token"); 

         if (idTokenHint != null) 
         { 
          n.ProtocolMessage.IdTokenHint = idTokenHint.Value; 
         } 

        } 

        return Task.FromResult(0); 
       } 
      } 

     }); 
    } 
} 

enter image description here

在IdSvr3客户端配置已被指定使用的混合流量和我已经检查客户端ID和客户端秘密很多时间,以验证它们是正确的。

这里是在服务器端,客户端配置:

enter image description here

+1

是什么身份服务器日志说? – rawel

+0

我在哪里可以找到身份服务器日志? – TejSoft

+1

https://identityserver.github.io/Documentation/docsv2/configuration/logging.html – rawel

我能够通过查看标识服务器生成的日志来解决问题。日志表示客户机密码不正确,当我多次检查该密码与身份服务器上显示的内容完全相同时。但后来我意识到秘密应该是真实的文本而不是散列的。该工作修改后的代码如下:

string ClientId = @"WebPortalDemo"; 
//string ClientSecret = @"aG90apW2+DbX1wVnwwLD+eu17g3vPRIg7p1OnzT14TE="; // Incorrect secret, didn't work 
string ClientSecret = @"love"; // Actual text entered as secret, worked 

信用:@rawel