【问题标题】:Wrong expiration time in Jwt Security Token in .net core.net 核心中 Jwt 安全令牌的过期时间错误
【发布时间】:2018-11-26 18:24:26
【问题描述】:

我正在使用 Jwt 安全令牌进行身份验证。

var token = new JwtSecurityToken(
            issuer: "mysite.com",
            audience: "mysite.com",
            notBefore: DateTime.Now,
            expires: DateTime.Now.AddDays(1),
            claims: claimsdata,
            signingCredentials: signInCred
            );

startup.cs

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
        {
            options.TokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuer = true,
                ValidateAudience = true,
                ValidateIssuerSigningKey = true,
                ValidIssuer = "mysite.com",
                ValidAudience = "mysite.com",
                ClockSkew=TimeSpan.Zero,
                IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("mykey"))
            };
        });

但是当我调试这段代码时,我得到了以下时间

但现在我的电脑时间是下午 12:58 ....

请帮我给令牌正确的到期时间..

【问题讨论】:

    标签: authentication jwt


    【解决方案1】:

    Datetime.Now 在 JWT 中返回 UTC 时间。这就是为什么你不能得到当地时间。如果您想更改为当地时间,请尝试将 UTC 时间更改为您的当地时间。

    【讨论】:

    • 是的,所以请更改此问题以解决其他人。谢谢。
    • @Tejas :如果此答案回答了您的问题,请单击复选标记考虑accepting this answer。这向更广泛的社区表明您找到了解决方案,并为回答者和您自己赢得了一些声誉。
    【解决方案2】:

    我实现了以下 sn-p 解决了我的查询...

            var date = DateTime.Now.ToString();
            DateTime convertedDate = DateTime.SpecifyKind(
                DateTime.Parse(date),
                DateTimeKind.Utc);
            DateTime dt = convertedDate.ToLocalTime();
    

    【讨论】:

      猜你喜欢
      • 2019-08-04
      • 2020-08-24
      • 2018-11-12
      • 2019-04-26
      • 2023-03-19
      • 2019-04-18
      • 2023-04-04
      • 1970-01-01
      • 2018-10-07
      相关资源
      最近更新 更多