无法静默获取令牌。调用方法 AcquireToken

Failed to acquire token silently. Call method AcquireToken

我正在尝试从 outlooo 365 获取一些日历项目 API

我从样本中得到的代码是这样的:

 public async Task<ActionResult> Index() {
      // fetch from stuff user claims
      var signInUserId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
      var userObjectId = ClaimsPrincipal.Current.FindFirst(SettingsHelper.ClaimTypeObjectIdentifier).Value;

      // discover contact endpoint
      var clientCredential = new ClientCredential(SettingsHelper.ClientId, SettingsHelper.ClientSecret);
      var userIdentifier = new UserIdentifier(userObjectId, UserIdentifierType.UniqueId);

      // create auth context
      AuthenticationContext authContext = new AuthenticationContext(SettingsHelper.AzureADAuthority, new EFADALTokenCache(signInUserId));

      // create O365 discovery client 
      DiscoveryClient discovery = new DiscoveryClient(new Uri(SettingsHelper.O365DiscoveryServiceEndpoint),
        async () => {
          var authResult = await authContext.AcquireTokenSilentAsync(SettingsHelper.O365DiscoveryResourceId, clientCredential, userIdentifier);

          return authResult.AccessToken;
        });

      // query discovery service for endpoint for 'calendar' endpoint
      var dcr = await discovery.DiscoverCapabilityAsync("Calendar");

      // create Outlook client using the calendar api endpoint
      OutlookServicesClient client = new OutlookServicesClient(dcr.ServiceEndpointUri,
        async () => {
          var authResult = await authContext.AcquireTokenSilentAsync(dcr.ServiceResourceId, clientCredential,
          userIdentifier);

          return authResult.AccessToken;
        });

      // get contacts
      var results = await client.Me.Events.Take(20).ExecuteAsync();
      ViewBag.Events = results.CurrentPage.OrderBy(c => c.Start);

      return View();
    }

这是基于此处提供的示例> https://github.com/OfficeDev/TrainingContent/tree/master/O3651/O3651-5%20Getting%20started%20with%20Office%20365%20APIs/Completed%20Projects

我得到这个错误:

静默获取令牌失败。调用方法 AcquireToken 说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。

异常详细信息:Microsoft.IdentityModel.Clients.ActiveDirectory.AdalSilentTokenAcquisitionException:无法静默获取令牌。调用方法 AcquireToken

来源错误:

Line 42:       OutlookServicesClient client = new OutlookServicesClient(dcr.ServiceEndpointUri,
Line 43:         async () => {
Line 44:           var authResult = await authContext.AcquireTokenSilentAsync(dcr.ServiceResourceId, clientCredential,
Line 45:           userIdentifier);

这是设置助手

public class SettingsHelper {

    public static string ClientId {
      get { return ConfigurationManager.AppSettings["ida:ClientID"]; }
    }

    public static string ClientSecret {
      get { return ConfigurationManager.AppSettings["ida:Password"]; }
    }

    public static string AzureAdTenantId {
      get { return ConfigurationManager.AppSettings["ida:TenantId"]; }
    }

    public static string O365DiscoveryServiceEndpoint {
      get { return "https://api.office.com/discovery/v1.0/me/"; }
    }

    public static string O365DiscoveryResourceId {
      get { return "https://api.office.com/discovery/"; }
    }

    public static string AzureAdGraphResourceId {
      get { return "https://graph.windows.net"; }
    }

    public static string AzureADAuthority {
      get { return string.Format("https://login.windows.net/{0}/", AzureAdTenantId); }
    }

    public static string ClaimTypeObjectIdentifier {
      get { return "http://schemas.microsoft.com/identity/claims/objectidentifier"; }
    }
  }

我很确定 clientid 和 secret 没问题,因为我已经在 azure AD 上创建了应用程序。

我将设置助手与我项目中的设置助手进行了比较,后者验证正确。唯一的区别是您在 AzureADAuthority 属性 的末尾有额外的“/”。

public static string AzureADAuthority {
  get {return string.Format("https://login.windows.net/{0}", AzureAdTenantId); }
}

不幸的是,这是 vs 2015 RC 上的错误,在 RTM 版本上,相同的代码工作得很好。

谢谢

作为提示,我在尝试使用 VS Community 2015 实现 Video REST API 演示应用程序时遇到了同样的错误。我可以修复它更新所有包,我注意到有一个与Microsoft 身份或类似的东西,可能是那个包导致了错误。

当我使用 VS Community 2013 Update 4 使用相同的代码时,我没有遇到任何问题。

此致