Added auth

This commit is contained in:
2026-05-29 16:02:34 +02:00
parent 335e1a785e
commit 39be51304b
13 changed files with 281 additions and 1 deletions
@@ -0,0 +1,31 @@
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.Options;
namespace WebApi.Authentication;
public static class AuthenticationExtensions
{
public static IServiceCollection AddAuthenticationConfiguration(
this IServiceCollection services,
IConfiguration configuration)
{
services
.AddOptionsWithValidateOnStart<AuthenticationConfiguration>()
.Bind(configuration.GetRequiredSection(AuthenticationConfiguration.SectionName))
.ValidateDataAnnotations();
services.AddSingleton(serviceProvider =>
serviceProvider.GetRequiredService<IOptions<AuthenticationConfiguration>>().Value);
services.AddScoped<JwtBearerEventsHandler>();
services.ConfigureOptions<JwtBearerOptionsSetup>();
services
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer();
services.AddAuthorization();
return services;
}
}