2017-05-25 44 views
1

我掙扎了最後4小時來解決這個問題這是走出這4行代碼:Azure活動目錄錯誤 - 由於在緩存中找不到令牌而無法靜默獲取令牌。調用方法AcquireToken

  string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value; 
      var authContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(Startup.authority, new NaiveSessionCache(userObjectID)); 
      var credential = new Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential(Startup.clientId, "SECRET_VALUE"); 
      var result = await authContext.AcquireTokenSilentAsync("https://MyDomain.onmicrosoft.com/TaskWebAPI", credential, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId)); 
  • 線1號不會導致成任何問題。
  • Line no 2-只有一個值。 Startup.authority是https://login.microsoftonline.com/myTenantName
  • Line no 3- ClientId是我的Web應用程序客戶端ID。並且取代SECRETVALUE是我在WebAPP中爲Key部分生成的關鍵之一。
  • 線沒有4-具有資源AppID項

這將是一個很大的幫助,如果任何你幫我調試這四條線。

回答

0

AcquireTokenSilentAsync(...)試圖讓您獲得一個令牌而不顯示任何用戶界面,如果無法顯示,將會失敗。它通過查看令牌緩存中的有效令牌來完成此操作,並在必要時刷新令牌。此處的操作是捕獲AdalException並使用acquireToken調用重試。

catch (AdalException e) { 
    if (e.ErrorCode == "failed_to_acquire_token_silently") { 
     // Do an AcquireToken call 
    } 
} 

Here's AcquireToken的所有不同的重載。

+0

你可以請分享一下打電話給誰。我在webApp內部,此時客戶端已經通過WebApp進行身份驗證。我無法在此處向用戶顯示UI。 – Pragmatic

+0

嘗試[this overload](https://docs.microsoft.com/en-us/active-directory/adal/microsoft.authenticationcontext#Microsoft_IdentityModel_Clients_ActiveDirectory_AuthenticationContext_AcquireTokenAsync_System_String_Microsoft_IdentityModel_Clients_ActiveDirectory_ClientCredential_)。 [這裏是](https://github.com/Azure-Samples/active-directory-dotnet-webapp-webapi-oauth2-useridentity)一個完整的Web應用程序示例在.NET中可能有所幫助。 –

+0

沒有用! – Pragmatic

相關問題