DotBased/DotBased.ASP.Auth/DotBasedAuthDependencyInjection.cs
2024-07-13 16:27:45 +02:00

28 lines
1.2 KiB
C#

using DotBased.ASP.Auth.Scheme;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Extensions.DependencyInjection;
namespace DotBased.ASP.Auth;
public static class DotBasedAuthDependencyInjection
{
/// <summary>
/// Use the DotBased authentication implementation
/// </summary>
/// <remarks>Use the app.UseAuthentication() and app.UseAuthorization()!</remarks>
/// <param name="services">Service colllection</param>
/// <param name="configurationAction">DotBased auth configuration</param>
public static void UseBasedAuth(this IServiceCollection services, Action<BasedAuthConfiguration>? configurationAction = null)
{
var config = new BasedAuthConfiguration();
configurationAction?.Invoke(config);
services.AddSingleton<BasedAuthConfiguration>(config);
services.AddScoped<AuthenticationStateProvider, BasedAuthenticationStateProvider>();
services.AddAuthentication(options =>
{
options.DefaultScheme = BasedAuthenticationHandler.AuthenticationScheme;
}).AddScheme<BasedAuthenticationHandlerOptions, BasedAuthenticationHandler>(BasedAuthenticationHandler.AuthenticationScheme, null);
services.AddAuthorization();
}
}