2016-12-14 1 views
0

현재 this example을 사용하여 C#의 Microsoft 그래프에 로그온합니다. 어떤 종류의 토큰이 내부적으로 사용되는지 잘 모르겠습니다. 따라서 내 주요 질문은 다음과 같습니다. 새로 고침 토큰의 타이밍에 대해 걱정해야합니까? 리프레시 토큰 수명 구성에 대해 this page이 발견되었습니다.이를 변경해야합니까? 신청서 세입자를 변경 한 경우, 신청서를에 등록한 세입자에게도 적립됩니까? 내가 조금 인증 과정을 수정, 그래서 https://github.com/Azure-Samples/active-directory-dotnet-graphapi-web에서 방법을 사용할 수 있습니다 Microsoft Graph API 멀티 테넌트 토큰 수명

AuthorizationCodeReceived = async (context) => 
         { 
          // We received an authorization code => get token. 
          var code = context.Code; 

          ClientCredential credential = new ClientCredential(clientId, appKey); 
          string tenantID = context.AuthenticationTicket.Identity.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value; 
          string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value; 

          Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext authContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(string.Format("https://login.microsoftonline.com/{0}", tenantID), new EFADALTokenCache(signedInUserID)); 
          AuthenticationResult result = await authContext.AcquireTokenByAuthorizationCodeAsync(
           code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path).TrimEnd('/')), credential, graphResourceID); 
         } 

:

internal class AuthenticationHelper 
    { 

     /// <summary> 
     ///  Async task to acquire token for Application. 
     /// </summary> 
     /// <returns>Async Token for application.</returns> 
     public static async Task<string> AcquireTokenAsync() 
     { 
      string clientId = ConfigurationManager.AppSettings["ida:ClientID"]; 
      string appKey = ConfigurationManager.AppSettings["ida:AppKey"]; 
      string graphResourceID = Constants.ResourceUrl; 
      string signedInUserID = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value; 
      string tenantID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value; 
      string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value; 

      // get a token for the Graph without triggering any user interaction (from the cache, via multi-resource refresh token, etc) 
      ClientCredential clientcred = new ClientCredential(clientId, appKey); 
      // initialize AuthenticationContext with the token cache of the currently signed in user, as kept in the app's EF DB 
      AuthenticationContext authContext = new AuthenticationContext(string.Format("https://login.microsoftonline.com/{0}", tenantID), new EFADALTokenCache(signedInUserID)); 
      AuthenticationResult result = await authContext.AcquireTokenSilentAsync(graphResourceID, clientcred, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId)); 
      return result.AccessToken; 
     } 

     /// <summary> 
     ///  Get Active Directory Client for Application. 
     /// </summary> 
     /// <returns>ActiveDirectoryClient for Application.</returns> 
     public static ActiveDirectoryClient GetActiveDirectoryClient() 
     { 
      string tenantID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value; 
      Uri baseServiceUri = new Uri(Constants.ResourceUrl); 
      ActiveDirectoryClient activeDirectoryClient = 
       new ActiveDirectoryClient(new Uri(baseServiceUri, tenantID), 
        async() => await AcquireTokenAsync()); 
      return activeDirectoryClient; 
     } 
    } 

을 (무기명

초기 인증이 (전체 코드 here)처럼 작동) 위의 코드에서 얻은 AccessToken은 요청 시간과 동일한 만료 날짜를 가지며 상당히 길기 때문에 인증 과정에서 실제로 일어나는 일은 나를위한 블랙 박스처럼 보이기 때문에 이해하고 싶습니다.

답변

0

짧은 대답 : 예.

긴 대답 : 사용중인 라이브러리 (ADAL)는 새로 고침 토큰 (따라서 사용자 세션)을 최대한 오래 유지할 수있는 모든 작업을 수행합니다. AcquireToken*에 전화 할 때마다 토큰 새로 고침이 처리됩니다. 그러나 결국 새로 고침 토큰이 만료됩니다. 그들이 만료 될 때 개발자가 제어 할 수 없습니다. 참조하는 링크를 사용하여 기본값을 설정할 수 있지만 이러한 기본값은 항상 고객이 무시할 수 있습니다. 사용자가 앱을 취소하거나 앱이 잠재적으로 악의적 인 것으로 확인 된 경우와 같이 다른 이유로 리 플래시 토큰을 사용할 수 없게 될 수도 있습니다.

그래서 ADAL이 토큰을 얻을 수없는 경우를 처리해야합니다. ADAL에서는 정상적으로 처리하고 다시 로그인하도록 사용자를 리디렉션 할 수있는 예외가 발생하며 새로 고침 토큰이 만료 된 상황을 해결할 수 있습니다.

+0

감사합니다. 이렇게하면 더 명확 해집니다. 고객이 토큰을 취소하는 데 문제가 없습니다. 하지만 내 광고 토큰 수명에 대한 설정이 내 응용 프로그램이 승인 된 고객 AD에 의해 재정의 될 수 있는지 확실하지 않습니다. 전자 메일 보관과 같은 몇 가지 배경 작업을 수행해야하므로 심각한 문제가 될 수 있습니다. – Compufreak

관련 문제