Initial commit - ERP
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
|
||||
namespace Commerce.Config;
|
||||
|
||||
public static class Authentication
|
||||
{
|
||||
public static IServiceCollection Authenticate(this IServiceCollection services, ConfigurationManager configuration)
|
||||
{
|
||||
services.AddAuthentication(x =>
|
||||
{
|
||||
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
}).AddJwtBearer(o =>
|
||||
{
|
||||
|
||||
var jwtKey = configuration["JWT:Key"] ?? throw new InvalidOperationException("JWT:Key is missing in configuration.");
|
||||
var Key = Encoding.UTF8.GetBytes(jwtKey);
|
||||
o.SaveToken = true;
|
||||
o.TokenValidationParameters = new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuer = false, // on production make it true
|
||||
ValidateAudience = false, // on production make it true
|
||||
ValidateLifetime = true,
|
||||
ValidateIssuerSigningKey = true,
|
||||
ValidIssuer = configuration["JWT:Issuer"],
|
||||
ValidAudience = configuration["JWT:Audience"],
|
||||
IssuerSigningKey = new SymmetricSecurityKey(Key),
|
||||
ClockSkew = TimeSpan.Zero
|
||||
};
|
||||
o.Events = new JwtBearerEvents
|
||||
{
|
||||
OnAuthenticationFailed = context =>
|
||||
{
|
||||
if (context.Exception.GetType() == typeof(SecurityTokenExpiredException))
|
||||
{
|
||||
context.Response.Headers.Append("IS-TOKEN-EXPIRED", "true");
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user