2017-12-05 13 views
2

私は各リクエストでAzure ADから取得したIdTokenを検証しようとしていますが、トークンに署名がないというエラーが表示され続けます。私がアクセストークンを検証するとき、それは動作しますが、私はむしろユーザーの主張を含むIdトークンを使用します。アズールが署名付きのイドトークンを送り返すようにするには、とにかくありますか?各リクエストでOpenId IdTokenを検証する

public JwtSecurityToken ValidateJwtToken(string jwtToken) 
    { 
     string stsDiscoveryEndpoint = $"{_authority}/.well-known/openid-configuration"; 

     ConfigurationManager<OpenIdConnectConfiguration> configManager = 
      new ConfigurationManager<OpenIdConnectConfiguration>(stsDiscoveryEndpoint, new OpenIdConnectConfigurationRetriever()); 

     OpenIdConnectConfiguration config = configManager.GetConfigurationAsync().Result; 

     TokenValidationParameters validationParameters = new TokenValidationParameters 
     { 
      ValidateAudience = false, 
      ValidateIssuer = false, 
      ValidateLifetime = false, 
      IssuerSigningKeys = config.SigningKeys 
     }; 

     JwtSecurityTokenHandler tokendHandler = new JwtSecurityTokenHandler(); 

     try 
     { 
      SecurityToken token; 
      tokendHandler.ValidateToken(jwtToken, validationParameters, out token); 

      return token as JwtSecurityToken; 
     } 
     catch (Exception ex) 
     { 
      loggingService.LogError("Could not validate azure ad token", nameof(AzureSecurityService), ex); 
      return null; 
     } 
    } 

    public async Task<string> GenerateToken(string code) 
    { 
     AuthenticationContext authenticationContext = new AuthenticationContext(_authority); 

     try 
     { 
      string baseUrl = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority); 
      AuthenticationResult result = 
       await authenticationContext.AcquireTokenByAuthorizationCodeAsync(code, new Uri(baseUrl), new ClientCredential(_clientId, _clientSecret)); 

      return result.IdToken; 
     } 
     catch (AdalException adalex) 
     { 
      loggingService.LogError("Could not get authorization request url", nameof(AzureSecurityService), adalex); 
      return null; 
     } 
     catch (Exception ex) 
     { 
      loggingService.LogError("Could not get authorization request url", nameof(AzureSecurityService), ex); 
      return null; 
     } 
    } 

    public async Task<string> GetAuthUrl() 
    { 
     AuthenticationContext authenticationContext = new AuthenticationContext(_authority); 

     // Config for OAuth client credentials 
     try 
     { 
      string baseUrl = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority); 
      var authUri = 
       await authenticationContext.GetAuthorizationRequestUrlAsync("00000000-0000-0000-0000-000000000000", _clientId, new Uri(baseUrl), UserIdentifier.AnyUser, null); 

      return authUri.ToString(); 
     } 
     catch (AdalException adalex) 
     { 
      loggingService.LogError("Could not get authorization request url", nameof(AzureSecurityService), adalex); 
      return null; 
     } 
     catch (Exception ex) 
     { 
      loggingService.LogError("Could not get authorization request url", nameof(AzureSecurityService), ex); 
      return null; 
     } 
    } 

UPDATE

私は主張を取り戻すた提供されている余地がなかったにもかかわらず、何らかの理由でそう。私が今行っていることがちょうどスコープのための認証URLにクエリパラメータを追加して、OpenIDのとプロファイルの両方を要求されます。

var authUri = 
       await authenticationContext.GetAuthorizationRequestUrlAsync("00000000-0000-0000-0000-000000000000", _clientId, new Uri(GetBaseUrl()), UserIdentifier.AnyUser, "scope=openid profile"); 

私の質問は、なぜ、デフォルトのスコープは署名でid_tokenを返しません。今は?

+0

ドキュメントを参照しようとしましたか?おそらく彼らはこの行動について何か言及しているかもしれません。 –

+1

私は文書を流しました...紛失している署名について何も言及していませんでした。 –

+0

あなたの承認要求には範囲値openidがありますか?事実、Azure OAuth 2.0のトークンレスポンスにはidトークンJWTが含まれています。 –

答えて

1

あなたはOpenID Connect specification

範囲

REQUIREDから、あなたの承認要求に

を、必要な範囲の値を置くことを忘れているはずです。 OpenID Connect要求には、openidスコープ値が含まれていなければなりません。 openidスコープの値が存在しない場合、その動作は完全に 未指定です。他のスコープ値が存在してもよい(MAY)。スコープ値は、 が実装によって理解されていないことを使用して無視すべきである(SHOULD)。この の仕様で定義されている追加のスコープ値については、 セクション5.4と11を参照してください。

だから、認証要求にopenidのを指定する必要があります。

AzureはOAuth 2.0トークンレスポンスのidトークンを返します(openidスコープが存在しない場合)。アズールAD OAuth2.0 documentationから

id_token

符号なしJSONウェブトークン(JWT)。このアプリは、このトークンの セグメントをbase64Urlでデコードして、 のユーザーに関する情報をリクエストできます。アプリは値をキャッシュして表示することができますが、 は認証やセキュリティの境界に頼るべきではありません。

ドキュメントによれば、エンドユーザーを表示するだけです。 authenticate/authorizeにこのidトークンを使用してはいけません。documentation

id_token

から

一方、適切なOpenIDの接続トークンの応答を、Auzreはあなたに署名したIDトークンを送信し、

アプリが要求されていることid_token 。あなたはid_token〜 を使ってユーザーの身元を確認し、ユーザーとのセッションを開始することができます。同じドキュメントの

検証id_tokenセクションでは、これはあなたの問題を解決したトークン

希望を検証する方法について説明します。