2017-01-24 13 views
0

私は thisサンプルアプリケーションを辿っており、大部分は問題なく動作します...しかし、私がテストの目的で作成したユーザーの場合、例外がスローされます以下のコードブロックは:Azure AD - AdalSilentTokenAcquisitionException

public static string LookupDisplayNameOfAADObject(string objectId) 
{ 
     string objectDisplayName = null; 
     string tenantId = (ClaimsPrincipal.Current).FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value; 
     string signedInUserID = (ClaimsPrincipal.Current).FindFirst(System.IdentityModel.Claims.ClaimTypes.NameIdentifier).Value; 
     string userObjectID = (ClaimsPrincipal.Current).FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value; 

     ClientCredential credential = new ClientCredential(ConfigurationManager.AppSettings["ida:ClientId"], ConfigurationManager.AppSettings["ida:ClientSecret"]); 


     AuthenticationContext authContext = new AuthenticationContext(string.Format(ConfigurationManager.AppSettings["ida:Authority"], tenantId), new ADALTokenCache(signedInUserID)); 

     string test = string.Format(ConfigurationManager.AppSettings["ida:Authority"], tenantId); 


     AuthenticationResult result = authContext.AcquireTokenSilent(ConfigurationManager.AppSettings["ida:GraphApiIdentifier"], credential, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId)); 



     HttpClient client = new HttpClient(); 

     string doQueryUrl = string.Format(
      "{0}{1}/directoryObjects/{2}?api-version={3}", 
      ConfigurationManager.AppSettings["ida:GraphApiIdentifier"], tenantId, 
      objectId, ConfigurationManager.AppSettings["ida:GraphApiVersion"]); 

     HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, doQueryUrl); 
     request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken); 
     HttpResponseMessage response = client.SendAsync(request).Result; 

     if (response.IsSuccessStatusCode) 
     { 
      var responseContent = response.Content; 
      string responseString = responseContent.ReadAsStringAsync().Result; 
      dynamic directoryObject = System.Web.Helpers.Json.Decode(responseString); 
      if (directoryObject != null) objectDisplayName = string.Format("{0} ({1})", directoryObject.displayName, directoryObject.objectType); 
     } 

     return objectDisplayName; 
} 
  • AcquireTokenSilentが
  • ...しかし、これだけの単一のユーザーの場合はAdalSilentTokenAcquisitionExceptionをスローした例外メッセージを指定は次のとおりである:「トークン黙っ取得に失敗しましたCallメソッドAcquireToken。」。

AcquireTokenメソッドは、ユーザーに再度ログインするように促します。ユーザーにプロンプ​​トを表示しないようにしようとしています。これは、この1人のユーザーの場合にのみ発生します。 この状況をどのように処理する必要がありますか?

答えて

1

この例外が発生した場合、ADALはアクセストークンまたはリフレッシュトークンを使用できるキャッシュに配置できなかったことを意味します。

トークンキャッシュがメモリ内にないようにしてください。プロセスが再起動された場合にはワイプされません。編集:私はDBにあるので、問題ではないはずですが参照してください。

セッションの継続時間を長くすることもできます。デフォルトでASP.NETは20分に制限されており、デフォルトでOpenIdConnectがそれに従います。それは、リフレッシュトークンがかなり長く使用可能であっても、20分後にそれらのトークンを消去するだけであることを意味します。あなたはそのようなStartup.Auth.csにOpenIdConnectミドルウェアの登録を変更する必要がありますそのために

app.UseOpenIdConnectAuthentication(
      new OpenIdConnectAuthenticationOptions 
      { 
       // ... Rest removed for brevity 
       UseTokenLifetime = false 
      }); 

そして、web.configファイルには、あなたが望むものにセッション時間を設定します。

<system.web> 
    <sessionState timeout="720" /><!-- 12 hour session duration --> 
</system.web> 

それ以外の場合は、暗黙の取得例外が発生した場合は、ユーザーがAADにリダイレクトされて再認証され、新しいアクセス権とリフレッシュトークンが取得されます。

+0

私が扱っていることについて、あなたが提供した答えは私には分かりませんでしたが、解決策は問題を解決しませんでした...このユーザーとして再ログしても、例外はスローされます。私がこのことについて最も気にしているのは、これがこの1人のユーザにしか起こらないという事実です...私はここでダミーのデータを使っているので、私はdbを削除して新しいものを開始します。 –

+0

Bummer :(トークンキャッシュをデバッグして、何が起こっているのかを確認する必要があります。トークンを見つけることができるはずですが、なんらかの理由で実行できません。 – juunas

+0

素晴らしい! – juunas