【问题标题】:Getting error implementing azure active directory dotnetcore webapp to webapi openidconnect将 Azure Active Directory dotnetcore webapp 实施到 webapi openidconnect 时出错
【发布时间】:2019-03-08 09:05:14
【问题描述】:

将 azure Active Directory dotnetcore webapp 实现到 webapi openidconnect 时出错

ClientCredential credential = new ClientCredential(AzureAdOptions.Settings.ClientId, AzureAdOptions.Settings.ClientSecret);
string userObjectID = (User.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier"))?.Value;
AuthenticationContext authContext = new AuthenticationContext(AzureAdOptions.Settings.Authority, new NaiveSessionCache(userObjectID, HttpContext.Session));
AuthenticationResult result = await authContext.AcquireTokenSilentAsync(AzureAdOptions.Settings.TodoListResourceId, credential, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));

遇到错误

ErrorCode: failed_to_acquire_token_silently
Microsoft.IdentityModel.Clients.ActiveDirectory.AdalSilentTokenAcquisitionException: Failed to acquire token silently as no token was found in the cache. Call method AcquireToken

【问题讨论】:

  • 你有答案了吗?

标签: azure .net-core openid access-token


【解决方案1】:

Microsoft.IdentityModel.Clients.ActiveDirectory.AdalSilentTokenAcquisitionException:无法静默获取令牌,因为在缓存中找不到令牌。调用方法 AcquireToken

如果no token is in the cacheAcquireTokenSilentAsync 将抛出一个AdalSilentTokenAcquisitionException,应用程序将需要调用AcquireTokenAsync

确保您的令牌缓存不在内存中,以便在进程重新启动时不会被擦除。 您可以做的另一件事是增加会话持续时间。默认情况下,ASP.NET 将其限制为 20 分钟,默认情况下 OpenIdConnect 会遵循此限制。这意味着它只会在 20 分钟后擦除这些令牌,即使刷新令牌的可用时间要长得多。

为此,您需要修改 Startup.Auth.cs 中的 OpenIdConnect 中间件注册,如下所示:

app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {
                // ... Rest removed for brevity
                UseTokenLifetime = false
            });

然后在 web.config 中将会话时间设置为您想要的:

<system.web>
  <sessionState timeout="720" /><!-- 12 hour session duration -->
</system.web>

更多详情可以参考这个article

【讨论】:

    猜你喜欢
    • 2017-02-08
    • 1970-01-01
    • 2015-12-17
    • 1970-01-01
    • 2015-05-06
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2019-12-24
    相关资源
    最近更新 更多