2017-01-24 1 views
0

나는 this 예제 응용 프로그램을 따라 왔으며 대부분의 경우 잘 작동합니다 ... 그러나 테스트 목적으로 만든 사용자 중 한 명에게는 예외가 발생합니다. 다음 코드 블록 :Azure 광고 - 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을 던졌습니다 만이 단일 사용자의 경우는 ...
  • 예외 메세지를 지정하지 않고있다 "토큰을 자동으로 획득하는 데 실패 호출 방법 AcquireToken을.".

AcquireToken 메서드는 사용자에게 다시 로그인하라는 메시지를 표시하며 사용자에게 묻는 것을 방지하기 위해 노력하고 있으며 명시된 것처럼이 한 사용자의 경우에만 발생합니다. 이 상황을 어떻게 처리해야합니까?

답변

1

이 예외가 발생하면 ADAL이 사용할 수있는 캐시에서 ADAL이 액세스 토큰 또는 새로 고침 토큰을 찾을 수 없음을 의미합니다.

토큰 캐시가 메모리에 있지 않도록하십시오. 그러면 프로세스가 다시 시작될 때 지워지지 않습니다. 편집 : 그것은 문제가 안된다 그래서 DB에있는 것을 참조하십시오.

세션 기간을 늘리면됩니다. 기본적으로 ASP.NET은이를 20 분으로 제한하며 기본적으로 OpenIdConnect가 그 다음에옵니다. 새로 고침 토큰이 훨씬 오래 사용할 수있는 경우에도 20 분 후에는 해당 토큰을 지우는 것입니다.

이를 위해 당신과 같이 Startup.Auth.cs에 OpenIdConnect 미들웨어 등록을 수정해야합니다 :에 세션 시간을 설정 Web.config의에서 다음

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

을 당신이 원하는 :

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

이외의 경우 자동 획득 예외가 발생하는 경우 사용자가 새 인증 및 토큰 새로 고침을 위해 재 인증하기 위해 사용자를 AAD로 리디렉션 할 수 있습니다.

+0

당신이 제공 한 답은 제가 다루고있는 것에 대한 통찰력을 주었지만 해결책은 문제를 해결하지 못했습니다 ...이 사용자로 다시 로그해도 예외는 여전히 발생합니다. 이 버그에 대해 가장 많이 알고있는 사실은이 사용자에 대해서만 발생한다는 사실입니다 ... 여기 더미 데이터로 작업하고 있기 때문에 데이터베이스를 삭제하고 새로운 데이터를 시작합니다. –

+0

Bummer : (토큰 캐시를 디버깅하고 토큰 캐시를 디버깅해야하며 토큰을 찾을 수 있어야하지만 어떤 이유로 든 수행 할 수 없습니다 .. – juunas

+0

좋아요! – juunas

관련 문제