0

我們使用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

回答

0

我能夠通過查看標識服務器生成的日誌來解決問題。日誌表示客戶機密碼不正確,當我多次檢查該密碼與身份服務器上顯示的內容完全相同時。但後來我意識到祕密應該是真實的文本而不是散列的。該工作修改後的代碼如下:

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

信用:@rawel

相關問題