33 lines
931 B
C#
33 lines
931 B
C#
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
using PrivaPub.Services;
|
|
|
|
using System.Text;
|
|
|
|
namespace PrivaPub.Extensions
|
|
{
|
|
public static class AddAuthExtension
|
|
{
|
|
public static AuthenticationBuilder AddPrivaPubAuth(this AuthenticationBuilder builder, IConfiguration configuration)
|
|
{
|
|
builder.AddJwtBearer(options => {
|
|
#if DEBUG
|
|
options.RequireHttpsMetadata = false;
|
|
#endif
|
|
options.TokenValidationParameters = new()
|
|
{
|
|
ValidateIssuer = true,
|
|
ValidateAudience = true,
|
|
ValidateLifetime = true,
|
|
ValidateIssuerSigningKey = true,
|
|
ValidIssuer = configuration["AppConfiguration:Jwt:Issuer"],
|
|
ValidAudience = configuration["AppConfiguration:Jwt:Audience"],
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["AppConfiguration:Jwt:Key"]))
|
|
};
|
|
options.Events = new JwtEvents();
|
|
});
|
|
return builder;
|
|
}
|
|
}
|
|
} |