【问题标题】:Assertion is not within its valid time range in AspNetCore Authentication OpenIdConnectAspNetCore Authentication OpenIdConnect 中的断言不在其有效时间范围内
【发布时间】:2021-03-15 01:18:53
【问题描述】:

我在尝试获取另一个应用程序的令牌时开始收到此错误。

其他信息:AADSTS70002:验证凭据时出错。 AADSTS50013:断言不在其有效时间范围内。

这在我从 AspNetCore 的 1.0.1 升级到 1.1.0 后开始发生。如果我清除我的 cookie,则此错误会消失一段时间。

以下是我用来获取此令牌的代码。它主要改编自 GitHub 上示例中的代码。

var userObjectId = (user.FindFirst(AuthSettings.UserObjectIdClaimName))?.Value;
AuthenticationResult authResult = null;
var authContext = GetAuthContext(userObjectId);
ClientCredential credential = new ClientCredential(AuthSettings.ClientId, AuthSettings.ClientSecret);
var claimsIdentity = user.Identity as ClaimsIdentity;

var token = claimsIdentity?.BootstrapContext as string;
if (token != null)
{
    try
    {
        authResult = await authContext.AcquireTokenAsync(appId, credential,
            new UserAssertion(token)); // Error here
    }
    catch (Exception) { }

}
if (authResult == null)
{
    // Error no token in cache here
    authResult = await authContext.AcquireTokenSilentAsync(appId, credential,
        new UserIdentifier(userObjectId, UserIdentifierType.UniqueId));
}


return authResult.AccessToken;

你知道有趣的是AcquireTokenSilentAsync 在第一次尝试失败时发生的永远不会起作用,因为令牌缓存是空的。在我的 Startup 中,我在 OnAuthorizationCodeReceived 中有代码,该代码继续并将令牌存储在令牌缓存中。此回调从不执行。大概如果第一批代码不起作用,那么如果此代码起作用,则回退将处理它。

app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
    ClientId = authSettings.ClientId,
    Authority = authSettings.Authority,
    ResponseType = OpenIdConnectResponseType.IdToken,
    PostLogoutRedirectUri = authSettings.PostLogoutUrl,
    TokenValidationParameters = new TokenValidationParameters()
    {
        SaveSigninToken = true
    },
    Events = new OpenIdConnectEvents
    {
        OnRemoteFailure = authHelper.CreateOnRemoteFailureRedirectHandler("/Home/Error"),
        OnAuthorizationCodeReceived =
            authHelper.CreateOnAuthorizationCodeRecievedAcquireAdditionalTokenHandler(new[] { CustomerManagerApi })
    },
});

这是当前没有为OnAuthorizationCodeRecieved 执行的代码:

var userObjectId = (context.Ticket?.Principal?.FindFirst(AuthSettings.UserObjectIdClaimName))?.Value;
var clientCred = new ClientCredential(AuthSettings.ClientId, AuthSettings.ClientSecret);
var authContext = new AuthenticationContext(AuthSettings.Authority, TokenCacheCreator(userObjectId));
var redirectAddressForAuthCode = new Uri(context.Properties.Items[OpenIdConnectDefaults.RedirectUriForCodePropertiesKey]);
authContext.AcquireTokenByAuthorizationCodeAsync(context.ProtocolMessage.Code, redirectAddressForAuthCode, clientCred, applicationId);

context.HandleCodeRedemption();

【问题讨论】:

    标签: asp.net-core openid-connect adal


    【解决方案1】:

    OnAuthorizationCodeReceived 未被触发的原因是因为您将ResponseType 设置为OpenIdConnectResponseType.IdToken,这将不会向客户端发送授权代码。

    其他信息:AADSTS70002:验证凭据时出错。 AADSTS50013:断言不在其有效时间范围内。

    对于这个错误,请确保访问令牌没有过期,然后再发送令牌请求,使用它进行用户断言。

    【讨论】:

    • 我降级回 Microsoft.AspNetCore.Authorization 1.0.0,我的所有问题都消失了。我现在也意识到 OnAuthorizationCodeReceived 甚至不是必需的,该示例正在使用它来登录以查询 GraphApi。我认为这只是一个错误。
    • 请在安全库中记录问题 :)
    【解决方案2】:

    我在交换用户断言以获取 Microsoft Graph API 范围令牌时遇到了同样的问题,我发现 .Net Core Web API 允许过期令牌在其到期后再过 5 分钟,因为默认的 5 分钟令牌验证时钟倾斜属性.

    所以我将 TokenValidationParameters 的 ClockSkew 属性设置为 TimeSpan.Zero,它阻止了过期的令牌请求,也解决了断言有效时间范围问题。

    【讨论】:

      猜你喜欢
      • 2020-06-22
      • 1970-01-01
      • 2014-12-19
      • 2018-11-27
      • 1970-01-01
      • 2018-10-19
      • 1970-01-01
      • 1970-01-01
      • 2020-03-10
      相关资源
      最近更新 更多