2016-04-07 12 views
2
var outlookServicesClient = await AuthenticationHelper.EnsureOutlookServicesClientCreatedAsync("Calendar"); 


internal static async Task<OutlookServicesClient> EnsureOutlookServicesClientCreatedAsync(string capabilityName) 
{ 
    var signInUserId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value; 
    var userObjectId = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value; 

    AuthenticationContext authContext = new AuthenticationContext(SettingsHelper.Authority, new ADALTokenCache(signInUserId)); 

    try 
    { 
     DiscoveryClient discClient = new DiscoveryClient(SettingsHelper.DiscoveryServiceEndpointUri, 
      async() => 
      { 
       var authResult = await authContext.AcquireTokenSilentAsync(SettingsHelper.DiscoveryServiceResourceId,                new ClientCredential(SettingsHelper.ClientId, SettingsHelper.ClientSecret), 
         new UserIdentifier(userObjectId, UserIdentifierType.UniqueId)); 

       return authResult.AccessToken; 
      }); 

      var dcr = await discClient.DiscoverCapabilityAsync(capabilityName); 

      return new OutlookServicesClient(dcr.ServiceEndpointUri, 
       async() => 
       { 
        var authResult = await authContext.AcquireTokenSilentAsync(dcr.ServiceResourceId, 
         new ClientCredential(SettingsHelper.ClientId, SettingsHelper.ClientSecret), 
         new UserIdentifier(userObjectId, UserIdentifierType.UniqueId)); 
        return authResult.AccessToken; 
       }); 
    } 
    catch (AdalException exception) 
    { 
     //Handle token acquisition failure 
     if (exception.ErrorCode == AdalError.FailedToAcquireTokenSilently) 
     { 
      authContext.TokenCache.Clear(); 
      throw exception; 
     } 
     return null; 
    } 
    public ADALTokenCache(string user) 
    { 
     // associate the cache to the current user of the web app 
     User = user; 
     this.AfterAccess = AfterAccessNotification; 
     this.BeforeAccess = BeforeAccessNotification; 
     this.BeforeWrite = BeforeWriteNotification; 

     // look up the entry in the DB 
     Cache = db.UserTokenCacheList.FirstOrDefault(c => c.webUserUniqueId == User); 
     // place the entry in memory 
     this.Deserialize((Cache == null) ? null : Cache.cacheBits); 
    } 

このコードをADAL認証に使用しています。これは、私のローカルIISサーバーで正常に動作しています。私がAZURE VMで同じものをホストしたときのようなエラーが表示された場合ADAL認証エラー

"トークンの取得に失敗しました。コールメソッドAcquireToken"誰もがこのエラーを解決する上で私を助けることができますか?

設定ヘルパーコードは次のとおりです。公開されているADALTokenCache(stringユーザー)では、ユーザーIDは細かくなっていますが、空のキャッシュを取得しています...理由は何ですか?

AuthenticationContext authContext = new AuthenticationContext(SettingsHelper.Authority, new ADALTokenCache(signInUserId)); 

     try 
     { 
      DiscoveryClient discClient = new DiscoveryClient(SettingsHelper.DiscoveryServiceEndpointUri, 
       async() => 
       { 
        var authResult = await authContext.AcquireTokenSilentAsync(SettingsHelper.DiscoveryServiceResourceId, 
                       new ClientCredential(SettingsHelper.ClientId, 
                            SettingsHelper.ClientSecret), 
                       new UserIdentifier(userObjectId, 
                            UserIdentifierType.UniqueId)); 

        return authResult.AccessToken; 
       }); 
+0

私は以下のURLをチェックしましたが、解決策はありません。どのようなソリューションを提案することができますか?https://social.msdn.microsoft.com/Forums/en-US/92438173-df6e-47ec-92f4-3cadf61b067a/azure-ad-failed-to-acquire-token-黙って?フォーラム= WindowsAzureAD – user3463733

+0

これは既にチェック済みですか?https://github.com/OfficeDev/O365-ASPNETMVC-Start/issues/28 – Aravind

+0

このリンクにも解決策はありません。ローカルマシンでは完璧に動作しますが、アジールのVMで問題が発生したときにホストされました。トークンは爽やかではないと思います。 – user3463733

答えて

1

権限に「共通」が含まれていないことを確認してください。また、http://www.cloudidentity.com/blog/2015/08/07/adal-diagnostics/の説明に従って診断をオンにし、トレースを見てください。非常に頻繁にこれはキャッシュの不一致によるものです - acquiretokensilentはキャッシュされたトークンでしか動作せず、キャッシュをシードしなかった場合/以前に選択したキャッシュインスタンスに対して作業していない場合/別のユーザー識別子を渡す/あなたはキャッシュミスを得るでしょう。

0

GithubでO365-ASPNETMVC-Startプロジェクトを使用していたとします。

Azure VMのweb.configファイルに「ida:TenantId」とは何を設定していますか?

「ida:TenantId」を「共通」に設定すると、「トークンを静かに取得できませんでした。コールメソッドAcquireToken」が表示されます。このシナリオでは、実際のテナントIDに「ida:TenantId」を設定する必要があります。たとえば、「e07xxxx0e-fxx2-441f-ad9a-9dxxa59xxx52」(guid)です。

+0

' <キーを追加= "ウェブページ:バージョン" 値= "3.0.0.0"/> <キー=追加 "Webページを:有効" 値= "偽"/> <キー= "ClientValidationEnabled" 値を追加=」 ClientSecret "値= "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:IDA "/> は、<キー=を追加" ClientIdを "値=" xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:真"/> <追加キー= "UnobtrusiveJavaScriptEnabled" 値は= "真IDA"/> <キー=を追加します" "/> key = "ida:AADInstance" value = "https://login.microsoftonline.com/" /> ' – user3463733

+0

上記はweb.coの設定ですあなたが間違っている場合は、一度それを確認して変更を提案できますか? – user3463733

+0

"http#// your_site/Account/RefreshSession"を呼び出してセッションをリフレッシュしようとしましたか? (と置換する ':')。 –