我正在按照此处的示例进行操作: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);
}