Tejas
Tejas

Reputation: 461

Wrong expiration time in Jwt Security Token in .net core

I am doing authentication using Jwt Security Token.

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"))
            };
        });

but when I am debugging this code I got following time

image

But Now my PC time is 12:58 PM ....

Please help me to give correct expiry timing to token..

Upvotes: 0

Views: 1897

Answers (2)

Tejas
Tejas

Reputation: 461

I implemented following snippet which solved my query...

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

Upvotes: 0

J X J
J X J

Reputation: 78

enter image description hereDatetime.Now in JWT return the UTC time. that's why you can't get local time. If you want to change to local time , try to change the UTC time to your local time.

Upvotes: 3

Related Questions