git-subtree-dir: Musify.WebApi git-subtree-mainline:9d433868ccgit-subtree-split:ca8750c3b4
32 lines
1001 B
C#
32 lines
1001 B
C#
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;
|
|
}
|
|
}
|