using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.IdentityModel.Tokens; using System.Text; namespace ShoppingAssistantApi.Infrastructure.InfrastructureExtentions; public static class JwtTokenAuthenticationExtention { public static IServiceCollection AddJWTTokenAuthentication(this IServiceCollection services, IConfiguration configuration) { services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = configuration.GetValue("JsonWebTokenKeys:ValidateIssuer"), ValidateAudience = configuration.GetValue("JsonWebTokenKeys:ValidateAudience"), ValidateLifetime = configuration.GetValue("JsonWebTokenKeys:ValidateLifetime"), ValidateIssuerSigningKey = configuration.GetValue("JsonWebTokenKeys:ValidateIssuerSigningKey"), ValidIssuer = configuration.GetValue("JsonWebTokenKeys:ValidIssuer"), ValidAudience = configuration.GetValue("JsonWebTokenKeys:ValidAudience"), IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration.GetValue("JsonWebTokenKeys:IssuerSigningKey"))), ClockSkew = TimeSpan.Zero }; }); return services; } }