"静默获取令牌失败.调用方法 AcquireToken" [英] "Failed to acquire token silently. Call method AcquireToken"

查看:57
本文介绍了"静默获取令牌失败.调用方法 AcquireToken"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试试{ClientCredential clientCredential = new ClientCredential("***********","**************");UserIdentifier userIdentifier = new UserIdentifier(userObjectId, UserIdentifierType.UniqueId);DiscoveryClient discClient = new DiscoveryClient(SettingsHelper.DiscoveryServiceEndpointUri,异步 () =>{var authResult = await authContext.AcquireTokenSilentAsync(SettingsHelper.DiscoveryServiceResourceId,clientCredential, userIdentifier);返回 authResult.AccessToken;});var dcr = await discClient.DiscoverCapabilityAsync(capabilityName);返回新的 OutlookServicesClient(dcr.ServiceEndpointUri,异步 () =>{var authResult = await authContext.AcquireTokenSilentAsync(dcr.ServiceResourceId, clientCredential, userIdentifier);返回 authResult.AccessToken;});}catch (AdalException 异常){//处理token获取失败if (exception.ErrorCode == AdalError.FailedToAcquireTokenSilently){authContext.TokenCache.Clear();抛出异常;}返回空;}

出现错误无法静默获取令牌.调用方法 AcquireToken"

解决方案

请确保应用已在 Office 365 租户 AD 下注册,否则您将无法发现和使用 Office 365 服务.

步骤#1 下载示例项目

步骤#3 在 web.config 文件中,从 Azure AD 门户复制 ClientId、TenantId 和 Client Secret.

Step#4 在 Visual Studio 中运行项目并使用 Azure AD 帐户登录

预期结果应如下:

如果您遇到令牌问题,也可以单击清除缓存数据库"按钮.

try
{
    ClientCredential clientCredential = new ClientCredential("***********","**************");
    UserIdentifier userIdentifier = new UserIdentifier(userObjectId, UserIdentifierType.UniqueId);

    DiscoveryClient discClient = new DiscoveryClient(SettingsHelper.DiscoveryServiceEndpointUri,
        async () =>
        {
            var authResult = await authContext.AcquireTokenSilentAsync(SettingsHelper.DiscoveryServiceResourceId,
                                                                       clientCredential, userIdentifier);

            return authResult.AccessToken;
        });

    var dcr = await discClient.DiscoverCapabilityAsync(capabilityName);

    return new OutlookServicesClient(dcr.ServiceEndpointUri,
        async () =>
        {
            var authResult = await authContext.AcquireTokenSilentAsync(dcr.ServiceResourceId, clientCredential, userIdentifier);
            return authResult.AccessToken;
        });
}
catch (AdalException exception)
{
    //Handle token acquisition failure
    if (exception.ErrorCode == AdalError.FailedToAcquireTokenSilently)
    {
        authContext.TokenCache.Clear();
        throw exception;
    }
    return null;
}

giving error "Failed to acquire token silently. Call method AcquireToken"

解决方案

Please ensure the app is registered under Office 365 tenant AD, else you will fail to discover and consume the Office 365 services.

Step#1 Download the sample project AspNetMvc-with-O365.zip.

Step#2 Register an app under Office 365 tenant AD, declare the Office 365 Exchange Online > Read user mail permission and set the REPLY URL as "http://localhost:2659/".

Step#3 In the web.config file, copy the ClientId, TenantId and Client Secret from Azure AD portal.

Step#4 Run the project in Visual Studio and log in with Azure AD account

The expected result should be as following:

You can also click the "Clear the cache database" button if you have acquire token issues.

这篇关于"静默获取令牌失败.调用方法 AcquireToken"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆