asp.net - ASP.Net 中的 JWT token 异常(生命周期验证失败。 token 缺少过期时间。)

标签 asp.net authentication authorization jwt lifetime

我正在 ASP 上创建自己的自定义身份验证。 Net MobileService 部署在 Azure 上。我使用 JWT token 。以下是我生成新 token 的方法(claimType = email):

    public static string GetSecurityToken(String email)
    {
        var symmetricKey = Convert.FromBase64String(signingKey);
        var tokenHandler = new JwtSecurityTokenHandler();

        var now = DateTime.UtcNow;
        var tokenDescriptor = new SecurityTokenDescriptor
        {
            Subject = new ClaimsIdentity(new[]
                    {
                    new Claim(ClaimTypes.Email, email)
                }),
            NotBefore = now,
            Expires = now.AddYears(10),
            Issuer = issuer,
            Audience = audience,
            IssuedAt = now,
            SigningCredentials = new SigningCredentials(
                new SymmetricSecurityKey(symmetricKey),
                SecurityAlgorithms.HmacSha256Signature),
        };

        var stoken = tokenHandler.CreateToken(tokenDescriptor);
        var token = tokenHandler.WriteToken(stoken);

        return token;
    }

token 被发送到客户端并存储。但是,当我尝试根据 token 授权消息时,出现错误:

Lifetime validation failed. The token is missing an Expiration Time.

这就是我尝试验证 token 的方式:

    public static ClaimsPrincipal GetPrincipal(string token)
    {
        try
        {
            var tokenHandler = new JwtSecurityTokenHandler();                
            var jwtToken = tokenHandler.ReadToken(token) as JwtSecurityToken;
            
            if (jwtToken == null)
                return null;

            var symmetricKey = Convert.FromBase64String(signingKey);

            Debug.WriteLine(String.Format("JWTManager > GetPrincipal > Validating Token: {0}", token));
            foreach (Claim claim in jwtToken.Claims)
            {
                Debug.WriteLine(String.Format("JWTManager > GetPrincipal > Claims: {0}", claim.ToString()));
            }
            
            var validationParameters = new TokenValidationParameters()
            {
                //RequireExpirationTime = true,
                //ValidateLifetime = true,
                ValidateIssuer = true,
                ValidateAudience = true,
                IssuerSigningKey = new SymmetricSecurityKey(symmetricKey),
            };
            
            SecurityToken securityToken;
            var principal = tokenHandler.ValidateToken(token, validationParameters, out securityToken);
            if (principal != null)
                Debug.WriteLine(String.Format("JWTManager > GetPrincipal > Principal: {0}", principal));
            return principal;
        }
        catch (SecurityTokenException ex)
        {
            Debug.WriteLine(String.Format("JWTManager > GetPrincipal: {0}", ex.Message));
            return null;
        }
        catch (Exception ex)
        {
            Debug.WriteLine(String.Format("JWTManager > GetPrincipal: {0}", ex.Message));
            return null;
        }
    }

执行tokenHandler.ValidateToken时抛出异常,并向principal返回null。

我的假设是,也许我没有正确设置 ExpiresIssuers 属性,并且 TokenHanlder 无法验证它们。但是,当我检查 jwtToken 时,所有声明均已正确设置。

这是完整的调试输出:

JWTManager > GetPrincipal > Claims: email: testEmail@email.com

JWTManager > GetPrincipal > Claims: nbf: 1494752301

JWTManager > GetPrincipal > Claims: exp: 33051661101

JWTManager > GetPrincipal > Claims: iat: 1494752301

JWTManager > GetPrincipal > Claims: iss: MASKED

JWTManager > GetPrincipal > Claims: aud: MAKSED

JWTManager > GetPrincipal: IDX10225: Lifetime validation failed. The token is missing an Expiration Time. Application: Tokentype:

最佳答案

@aha,看来您通过将到期日期时间缩短到 future 一年来解决了您的问题。这是有效的,但是如果您想了解它之前失败的底层架构原因(并因此在您自己的代码中进行适当的架构更改),您可以在此处阅读这篇文章:https://stackoverflow.com/a/46654832/1222775 .

最重要的是,针对 Microsoft owin 中间件验证的 JWT 的过期日期的上限为 2147483647(也恰好是 Int32.MaxValue),这意味着:2038 年 1 月 19 日星期二 03:14:07 GMT

在您的 SO 问题中,您发布的调试输出显示您使用的“exp”声明值,其值为 33051661101,可转换为:3017 年 5 月 14 日星期三 8: 58:21 AM 这超出了 Microsoft 的 exp 值上限近 80 年:)。

我希望微软能够尽快解决这个问题,但同时对于任何遇到类似问题的人来说,尽量不要发行太持久的代币,或者至少不要让它超过 2038 年 1 月 19 日星期二@格林威治标准时间 03:14:07 :)。

关于asp.net - ASP.Net 中的 JWT token 异常(生命周期验证失败。 token 缺少过期时间。),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43962567/

相关文章:

c# - asp插入错误

c# - 在 ASP.NET mvc5 项目中集成 google analytics api

asp.net - 不使用 OAuth/外部身份验证提供程序时,ASP.Net 身份声明有用吗?

java - 出现致命异常 : main error

asp.net - 如何允许匿名用户浏览Style文件夹

asp.net - 如何让用户保持登录状态 2 周?

c# - 在 ASP.NET Core 中使用 Cookie 进行身份验证

authentication - 如何让 Swagger UI 让我提供身份验证 header ?

objective-c - 在登录项启动具有 root 授权的应用程序?

c# - Bearer 被 IdentityServer4 中的授权过滤器禁止