refactor: Use ClockSkew options For prevent add default value (5min) to life time token

This commit is contained in:
Pc 2023-06-25 13:41:22 +03:30
parent 0b47559441
commit bb7aaa2edb
2 changed files with 9 additions and 5 deletions

View File

@ -5,6 +5,7 @@ using Microsoft.Extensions.DependencyInjection;
namespace BuildingBlocks.Jwt; namespace BuildingBlocks.Jwt;
using Duende.IdentityServer.EntityFramework.Entities; using Duende.IdentityServer.EntityFramework.Entities;
using Microsoft.IdentityModel.Tokens;
public static class JwtExtensions public static class JwtExtensions
{ {
@ -20,7 +21,11 @@ public static class JwtExtensions
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options => .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
{ {
options.Authority = jwtOptions.Authority; options.Authority = jwtOptions.Authority;
options.TokenValidationParameters.ValidateAudience = false; options.TokenValidationParameters = new TokenValidationParameters
{
ValidateAudience = false,
ClockSkew = TimeSpan.FromSeconds(2) // For prevent add default value (5min) to life time token!
};
options.RequireHttpsMetadata = jwtOptions.RequireHttpsMetadata; options.RequireHttpsMetadata = jwtOptions.RequireHttpsMetadata;
options.MetadataAddress= jwtOptions.MetadataAddress; options.MetadataAddress= jwtOptions.MetadataAddress;
}); });

View File

@ -44,14 +44,11 @@ public static class Config
new() new()
{ {
ClientId = "client", ClientId = "client",
AllowedGrantTypes = GrantTypes.ResourceOwnerPassword, AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
ClientSecrets = ClientSecrets =
{ {
new Secret("secret".Sha256()) new Secret("secret".Sha256())
}, },
AllowedScopes = AllowedScopes =
{ {
IdentityServerConstants.StandardScopes.OpenId, IdentityServerConstants.StandardScopes.OpenId,
@ -60,7 +57,9 @@ public static class Config
Constants.StandardScopes.PassengerApi, Constants.StandardScopes.PassengerApi,
Constants.StandardScopes.BookingApi, Constants.StandardScopes.BookingApi,
Constants.StandardScopes.IdentityApi Constants.StandardScopes.IdentityApi
} },
AccessTokenLifetime = 3600, // authorize the client to access protected resources
IdentityTokenLifetime = 3600 // authenticate the user
} }
}; };
} }