0

我正在按照此处的示例进行操作:Office 365 API 入门

当我的控制器操作执行时,它挂在 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);
}
4

1 回答 1

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

于 2015-06-10T22:12:37.713 回答