2017-01-20 8 views
0

Google은 일반 OpenID Connect 미들웨어를 사용하여 Google을 IdentityServer3을 사용하는 외부 ID 공급자로 사용하고 있습니다. 우리는 MetadataAddress 또는 특별한 TokenValidationParameters를 설정하지 않습니다 (그래서 Authority를 ​​기반으로하는 메타 데이터를 얻은 다음이를 기반으로 매개 변수를 채워야합니다). 다음 오류가 간헐적으로 발생합니다. 나는이 오류가 잘못된 사용자 지정 유효성 검사를 포함하는 것으로 보였고 간헐적이지는 않습니다.OWIN의 SecurityTokenSignatureKeyNotFoundException Google에 연결하는 OpenID Connect 미들웨어

Authentication Failed : Microsoft.IdentityModel.Protocols.OpenIdConnectMessage : System.IdentityModel.Tokens.SecurityTokenSignatureKeyNotFoundException : IDX10500 : Signature validation failed.Unable to resolve SecurityKeyIdentifier : 'SecurityKeyIdentifier 
(
IsReadOnly = False, 
Count = 1, 
Clause[0] = System.IdentityModel.Tokens.NamedKeySecurityKeyIdentifierClause 
) 
', 
token : '{"alg":"RS256","kid":"74e0db263dbc69ac75d8bf0853a15d05e04be1a2"}.{"iss":"https://accounts.google.com","iat":1484922455,"exp":1484926055, <snip more claims>}'. 
at System.IdentityModel.Tokens.JwtSecurityTokenHandler.ValidateSignature(String token, TokenValidationParameters validationParameters)in c : \ workspace \ WilsonForDotNet45Release \ src \ System.IdentityModel.Tokens.Jwt \ JwtSecurityTokenHandler.cs : line 943 
at System.IdentityModel.Tokens.JwtSecurityTokenHandler.ValidateToken(String securityToken, TokenValidationParameters validationParameters, SecurityToken & validatedToken)in c : \ workspace \ WilsonForDotNet45Release \ src \ System.IdentityModel.Tokens.Jwt \ JwtSecurityTokenHandler.cs : line 671 
at Microsoft.IdentityModel.Extensions.SecurityTokenHandlerCollectionExtensions.ValidateToken(SecurityTokenHandlerCollection tokenHandlers, String securityToken, TokenValidationParameters validationParameters, SecurityToken & validatedToken)in c : \ workspace \ WilsonForDotNet45Release \ src \ Microsoft.IdentityModel.Protocol.Extensions \ SecurityTokenHandlerCollectionExtensions.cs : line 71 
at Microsoft.Owin.Security.OpenIdConnect.OpenIdConnectAuthenticationHandler. <AuthenticateCoreAsync> d__1a.MoveNext() 

kidhttps://www.googleapis.com/oauth2/v3/certs에서 3 현재 키 2이다 칭함.

우리의 옵션은 다음과 같이 :

var options = new OpenIdConnectAuthenticationOptions 
       { 
        AuthenticationType = "Google", 
        Caption = "Sign in with Google", 
        Scope = "openid email profile", 
        ClientId = clientId, 
        Authority = "https://accounts.google.com/", 
        AuthenticationMode = AuthenticationMode.Passive, 
        RedirectUri = new Uri(baseUri, "identity/signin-google").ToString(), 
        Notifications = new OpenIdConnectAuthenticationNotifications() 
        { 
         AuthenticationFailed = context => HandleException(context), 
         RedirectToIdentityProvider = context => AddLoginHint(context), 
        }, 
        SignInAsAuthenticationType = signInAsType 
       }; 

       app.UseOpenIdConnectAuthentication(options); 

이이 구성 문제 또는과 (와 방법 그렇다면) 처리 될 필요가 일시적인 오류의 일종인가? 최종 고객은 한 번 재 시도를하고 있지만 (나는 기다리고 있다고 생각하지 않지만) 도움이되지는 않습니다.

답변

0

여기에서 문제는 기본값 ConfigurationManager이 5 일 동안 결과를 캐시하는 반면 Google은 키를 더 자주 (매일 같이) 롤백한다는 사실 때문인 것 같습니다. OWIN 미들웨어의 기본 동작을 사용하면 인식 할 수없는 키가있는 첫 번째 요청이 실패하고 다음 요청에서 키를 새로 고칩니다.

더 빠른 AutomaticRefreshInterval을 사용하여 자신의 ConfigurationManager에서 솔루션을 전달해야합니다. 아래 설정의 대부분은 다음과 같습니다. OpenIdConnectAuthenticationMiddleware

private static void ConfigureIdentityProviders(IAppBuilder app, string signInAsType) 
    { 
     var baseUri = new Uri("https://localhost:44333"); 
     var googleAuthority = "https://accounts.google.com/"; 
     var metadataAddress = googleAuthority + ".well-known/openid-configuration"; 
     var httpClient = new HttpClient(new WebRequestHandler()); 
     httpClient.Timeout = TimeSpan.FromMinutes(1); 
     httpClient.MaxResponseContentBufferSize = 1024 * 1024 * 10; // 10 MB 
     var configMgr = new ConfigurationManager<OpenIdConnectConfiguration>(metadataAddress, httpClient) 
          { 
           // Default is 5 days, while Google is updating keys daily 
           AutomaticRefreshInterval 
            = 
            TimeSpan 
            .FromHours(12) 
          }; 

     var options = new OpenIdConnectAuthenticationOptions 
          { 
           AuthenticationType = "Google", 
           Caption = "Google", 
           Scope = "openid email profile", 
           ClientId = GoogleClientId, 
           Authority = googleAuthority, 
           ConfigurationManager = configMgr, 
           AuthenticationMode = AuthenticationMode.Passive, 
           RedirectUri = 
            new Uri(baseUri, "identity/signin-google").ToString(), 
           SignInAsAuthenticationType = signInAsType 
          }; 

     app.UseOpenIdConnectAuthentication(options); 
    }