41 lines
1.4 KiB
C#
41 lines
1.4 KiB
C#
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using System.Text;
|
|
|
|
namespace JwtAuthManager
|
|
{
|
|
public static class CustomJwtAuthExtension
|
|
{
|
|
|
|
//private readonly ILogger<CustomJwtAuthExtension> _logger;
|
|
|
|
//public CustomJwtAuthExtension(ILogger<> logger)
|
|
//{
|
|
// _logger = (ILogger<CustomJwtAuthExtension>?)logger;
|
|
//}
|
|
|
|
public static void AddCustomJwtAuthentication(this IServiceCollection services)
|
|
{
|
|
|
|
services.AddAuthentication(options =>
|
|
{
|
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
}).AddJwtBearer(options =>
|
|
{
|
|
options.IncludeErrorDetails = true;
|
|
options.RequireHttpsMetadata = true;
|
|
options.SaveToken = true;
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateIssuer = false,
|
|
ValidateAudience = false,
|
|
ValidateIssuerSigningKey = true,
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(JwtTokenHandler.JWT_SECURITY_KEY))
|
|
};
|
|
});
|
|
}
|
|
}
|
|
}
|