SocialPub/PrivaPub/Extensions/AddAuthExtension.cs

33 lines
931 B
C#
Raw Normal View History

2023-02-18 08:52:17 +01:00
using Microsoft.AspNetCore.Authentication;
using Microsoft.IdentityModel.Tokens;
2023-02-19 00:43:43 +01:00
using PrivaPub.Services;
2023-02-18 08:52:17 +01:00
using System.Text;
2023-02-19 00:43:43 +01:00
namespace PrivaPub.Extensions
2023-02-18 08:52:17 +01:00
{
public static class AddAuthExtension
{
2023-02-19 00:43:43 +01:00
public static AuthenticationBuilder AddPrivaPubAuth(this AuthenticationBuilder builder, IConfiguration configuration)
2023-02-18 08:52:17 +01:00
{
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;
}
}
}