2016-12-14 10 views
0

現在、this exampleを使用してC#のMicrosoftグラフにログオンしています。私は内部的にどのような種類のトークンが使用されているのかよく分かりませんので、私の主な質問はです。リフレッシュトークンがタイムアウトするのを心配する必要はありますか? リフレッシュトークンの有効期間の設定については、this pageが見つかりました。これを変更する必要がありますか? また、アプリケーションテナントで変更した場合、テナントにも設定されますか?に申請を登録しますか? Iビット認証プロセスを変更し、私は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上記のコードから取得したExpirationDateはリクエストの時間と同じで、かなり長いため、認証処理中に実際に何が起きるのかは私のブラックボックスのように思えます。

答えて

0

短い回答:はい。

長い回答:使用しているライブラリ(ADAL)は、リフレッシュトークン(したがってユーザーのセッション)を可能な限り長く有効にするためにできるすべてを行います。 AcquireToken*に電話をかけるたびにトークンの更新が処理されます。ただし、最終的にリフレッシュトークンは期限切れになります。彼らが期限切れになったときは、あなたのコントロールから外れています(開発者として)。参照するリンクを使用してデフォルトを設定できますが、これらのデフォルトはいつでも顧客によって上書きできます。また、ユーザーがあなたのアプリを取り消した場合や、あなたのアプリが潜在的に悪質であると特定された場合など、リフレッシュトークンは無効になる可能性があります。

したがって、ADALがあなたのためにトークンを取得できない場合を処理する必要があります。 ADALでは、これにより、正常に処理して再度サインインするようにユーザーをリダイレクトする例外が発生し、リフレッシュトークンが期限切れになった原因を取り除くことができます。

+0

ありがとうございます。これにより、より明確になります。お客様がトークンを取り消すことに問題はありません。しかし、私の広告の設定がトークンライフタイムのために私のアプリケーションが認可されている顧客ADによって上書きできるかどうか分からないのですか?私は電子メールのアーカイブのようないくつかのバックグラウンドタスクを実行する必要があるかもしれないので、これは深刻な問題になる可能性があります。 – Compufreak

関連する問題