获取令牌时,Office365 API OutlookServicesClient挂起

时间:2015-05-15 13:39:26

标签: c# azure async-await office365 office365-apps

我正在关注此处的示例:Get started with Office 365 APIs

当我的控制器操作执行时,它会挂起var“new OutlookServicesClient”中的以下行

var authResult = await authContext.AcquireTokenSilentAsync(
    dcr.ServiceResourceId,
    new ClientCredential(this.configuration.IdaClientID, this.configuration.IdaClientSecret),
    new UserIdentifier(userObjectId, UserIdentifierType.UniqueId));

我无法弄清楚它为什么会挂起,尤其是因为AcquireTokenSilentAsync在此调用之前在发现客户端中运行良好。

非常感谢任何帮助。

我的控制器操作方法:

[Authorize]
public async Task<ActionResult> Index()
{    
    var contacts = new List<ContactItem>();
    var signInUserId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
    var userObjectId = ClaimsPrincipal.Current.FindFirst(ClaimTypesAdditions.ObjectIdentifier).Value;

    var authContext = new AuthenticationContext(this.configuration.IdaAuthority, new AdalTokenCache(signInUserId));

    try
    {
        var discClient = new DiscoveryClient(
            new Uri(this.configuration.Office365DiscoveryServiceEndpoint),
            async () =>
            {
                var authResult = await authContext.AcquireTokenSilentAsync(
                    this.configuration.Office365DiscoveryResourceID,
                    new ClientCredential(this.configuration.IdaClientID, this.configuration.IdaClientSecret),
                    new UserIdentifier(userObjectId, UserIdentifierType.UniqueId));

                return authResult.AccessToken;
            });

        var dcr = await discClient.DiscoverCapabilityAsync("Contacts");

        var exClient = new OutlookServicesClient(
            dcr.ServiceEndpointUri,
            async () =>
            {
                var authResult = await authContext.AcquireTokenSilentAsync(
                    dcr.ServiceResourceId,
                    new ClientCredential(this.configuration.IdaClientID, this.configuration.IdaClientSecret),
                    new UserIdentifier(userObjectId, UserIdentifierType.UniqueId));

                return authResult.AccessToken;
            });

        var contactsResult = await exClient.Me.Contacts.ExecuteAsync();

        do
        {
            var c = contactsResult.CurrentPage;
            contacts.AddRange(c.Select(contact => new ContactItem { FirstName = contact.GivenName }));

            contactsResult = await contactsResult.GetNextPageAsync();
        }
        while (contactsResult != null);
    }
    catch (AdalException exception)
    {
        if (exception.ErrorCode == AdalError.FailedToAcquireTokenSilently)
        {
            authContext.TokenCache.Clear();
        }
    }

    return this.View("Index", contacts);
}

1 个答案:

答案 0 :(得分:0)

1.0.34版Microsoft.Office365.OutlookServices.Portable中存在一个错误,导致OutlookServiceClient出现死锁。恢复到1.0.22似乎有效。

这里记录了https://github.com/OfficeDev/O365-ASPNETMVC-Start/commit/b5652864756636a0b141c222e964aba953357e7a#diff-04c6e90faac2675aa89e2176d2eec7d8R139

看起来将在下一个版本中修复,如此处所示 https://github.com/Microsoft/Vipr/commit/aaeff5cb94204c23d7501be8fa74b0260ddc52d9