DotBased/DotBased.ASP.Auth/DotBasedAuthDependencyInjection.cs

59 lines
2.9 KiB
C#
Raw Normal View History

2024-07-27 16:07:13 +02:00
using DotBased.ASP.Auth.Scheme;
using DotBased.ASP.Auth.Services;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Components.Authorization;
2024-07-13 16:27:45 +02:00
using Microsoft.Extensions.DependencyInjection;
namespace DotBased.ASP.Auth;
public static class DotBasedAuthDependencyInjection
{
/// <summary>
/// Use the DotBased authentication implementation
/// </summary>
2024-09-27 02:38:18 +02:00
/// <remarks>Use UseBasedServerAuth()!</remarks>
/// <param name="services">Service collection</param>
2024-07-13 16:27:45 +02:00
/// <param name="configurationAction">DotBased auth configuration</param>
2024-07-27 16:07:13 +02:00
public static IServiceCollection AddBasedServerAuth(this IServiceCollection services, Action<BasedAuthConfiguration>? configurationAction = null)
2024-07-13 16:27:45 +02:00
{
2024-07-27 16:07:13 +02:00
var Configuration = new BasedAuthConfiguration();
configurationAction?.Invoke(Configuration);
services.AddSingleton<BasedAuthConfiguration>(Configuration);
2024-09-27 02:38:18 +02:00
if (Configuration.AuthDataRepositoryType == null)
throw new ArgumentNullException(nameof(Configuration.AuthDataRepositoryType), $"No '{nameof(IAuthDataRepository)}' configured!");
services.AddScoped(typeof(IAuthDataRepository), Configuration.AuthDataRepositoryType);
2024-07-27 16:07:13 +02:00
if (Configuration.SessionStateProviderType == null)
throw new ArgumentNullException(nameof(Configuration.SessionStateProviderType), $"No '{nameof(ISessionStateProvider)}' configured!");
services.AddScoped(typeof(ISessionStateProvider), Configuration.SessionStateProviderType);
2024-09-27 02:38:18 +02:00
services.AddSingleton<AuthDataCache>();
2024-07-27 16:07:13 +02:00
services.AddScoped<AuthService>();
services.AddScoped<AuthenticationStateProvider, BasedServerAuthenticationStateProvider>();
services.AddAuthentication(options =>
{
options.DefaultScheme = BasedAuthenticationHandler.AuthenticationScheme;
}).AddScheme<BasedAuthenticationHandlerOptions, BasedAuthenticationHandler>(BasedAuthenticationHandler.AuthenticationScheme, null);
services.AddAuthorization();
services.AddCascadingAuthenticationState();
return services;
}
public static WebApplication UseBasedServerAuth(this WebApplication app)
{
app.UseAuthentication();
app.UseAuthorization();
// Data
var authConfig = app.Services.GetService<BasedAuthConfiguration>();
if (authConfig == null)
throw new NullReferenceException($"{nameof(BasedAuthConfiguration)} is null!");
2024-09-27 02:38:18 +02:00
if (authConfig.AuthDataRepositoryType == null)
throw new NullReferenceException($"{nameof(authConfig.AuthDataRepositoryType)} is null, cannot instantiate an instance of {nameof(IAuthDataRepository)}");
var dataProvider = (IAuthDataRepository?)Activator.CreateInstance(authConfig.AuthDataRepositoryType);
2024-07-27 16:07:13 +02:00
if (dataProvider != null) authConfig.SeedData?.Invoke(dataProvider);
return app;
2024-07-13 16:27:45 +02:00
}
}