如何使用c#

时间:2017-03-09 09:11:20

标签: c# asp.net-mvc-5 office365api

我们使用Azure AD进行身份验证并获取刷新的访问令牌。我们调用以下方法来获取Office 365用户联系人

但是当执行上述操作时,我们会遇到异常,例如" failed_to_acquire_token_silently"

任何人都可以帮助我们做错了!

List<string> myContacts = new List<string>();

        var signInUserId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
        var userObjectId = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;

        Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext authContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(SettingsHelper.Authority, new ADALTokenCache(signInUserId));

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

                     return authResult.AccessToken;
                 });

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

            Microsoft.Office365.OutlookServices.OutlookServicesClient exClient = new Microsoft.Office365.OutlookServices.OutlookServicesClient(dcr.ServiceEndpointUri,
                async () =>
                {
                    var authResult = await authContext.AcquireTokenSilentAsync(SettingsHelper.DiscoveryServiceResourceId,
                                                                                new Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential(SettingsHelper.ClientId,
                                                                                                     SettingsHelper.ClientSecret),
                                                                                new Microsoft.IdentityModel.Clients.ActiveDirectory.UserIdentifier(userObjectId,
                                                                                                   Microsoft.IdentityModel.Clients.ActiveDirectory.UserIdentifierType.UniqueId));

                    return authResult.AccessToken;
                });

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

            do
            {
                var contacts = contactsResult.CurrentPage;
                foreach (var contact in contacts)
                {
                    myContacts.Add(new MyContact { Name = contact.DisplayName });
                }

                contactsResult = await contactsResult.GetNextPageAsync();

            } while (contactsResult != null);
        }
        catch (Microsoft.IdentityModel.Clients.ActiveDirectory.AdalException exception)
        {

            //handle token acquisition failure
            if (exception.ErrorCode == Microsoft.IdentityModel.Clients.ActiveDirectory.AdalError.FailedToAcquireTokenSilently)
            {
                authContext.TokenCache.Clear();

                //handle token acquisition failure
            }
        }

        return View(myContacts);

提前致谢

0 个答案:

没有答案