Implementing AuthDataCache

This commit is contained in:
Max
2024-09-27 02:38:18 +02:00
parent c092b8a679
commit 0fed89e140
9 changed files with 218 additions and 154 deletions

View File

@@ -11,24 +11,23 @@ public static class DotBasedAuthDependencyInjection
/// <summary>
/// Use the DotBased authentication implementation
/// </summary>
/// <remarks>Use the app.UseAuthentication() and app.UseAuthorization()!</remarks>
/// <remarks>Use UseBasedServerAuth()!</remarks>
/// <param name="services">Service collection</param>
/// <param name="configurationAction">DotBased auth configuration</param>
public static IServiceCollection AddBasedServerAuth(this IServiceCollection services, Action<BasedAuthConfiguration>? configurationAction = null)
{
/*var authBuilder = new BasedAuthBuilder(services, configurationAction);
return authBuilder;*/
var Configuration = new BasedAuthConfiguration();
configurationAction?.Invoke(Configuration);
services.AddSingleton<BasedAuthConfiguration>(Configuration);
if (Configuration.AuthDataProviderType == null)
throw new ArgumentNullException(nameof(Configuration.AuthDataProviderType), $"No '{nameof(IAuthDataProvider)}' configured!");
services.AddScoped(typeof(IAuthDataProvider), Configuration.AuthDataProviderType);
if (Configuration.AuthDataRepositoryType == null)
throw new ArgumentNullException(nameof(Configuration.AuthDataRepositoryType), $"No '{nameof(IAuthDataRepository)}' configured!");
services.AddScoped(typeof(IAuthDataRepository), Configuration.AuthDataRepositoryType);
if (Configuration.SessionStateProviderType == null)
throw new ArgumentNullException(nameof(Configuration.SessionStateProviderType), $"No '{nameof(ISessionStateProvider)}' configured!");
services.AddScoped(typeof(ISessionStateProvider), Configuration.SessionStateProviderType);
services.AddSingleton<AuthDataCache>();
services.AddScoped<AuthService>();
services.AddScoped<AuthenticationStateProvider, BasedServerAuthenticationStateProvider>();
@@ -50,9 +49,9 @@ public static class DotBasedAuthDependencyInjection
var authConfig = app.Services.GetService<BasedAuthConfiguration>();
if (authConfig == null)
throw new NullReferenceException($"{nameof(BasedAuthConfiguration)} is null!");
if (authConfig.AuthDataProviderType == null)
throw new NullReferenceException($"{nameof(authConfig.AuthDataProviderType)} is null, cannot instantiate an instance of {nameof(IAuthDataProvider)}");
var dataProvider = (IAuthDataProvider?)Activator.CreateInstance(authConfig.AuthDataProviderType);
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);
if (dataProvider != null) authConfig.SeedData?.Invoke(dataProvider);
return app;