mirror of
https://github.com/hmaxnl/DotBased.git
synced 2025-01-18 18:14:20 +01:00
Added ISessionStateProvider interface & base implementation
This commit is contained in:
parent
c7d654a0ba
commit
2b17ed4cd7
|
@ -1,6 +0,0 @@
|
||||||
namespace DotBased.ASP.Auth;
|
|
||||||
|
|
||||||
public class AuthService
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
31
DotBased.ASP.Auth/BasedAuthBuilder.cs
Normal file
31
DotBased.ASP.Auth/BasedAuthBuilder.cs
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
using DotBased.ASP.Auth.Scheme;
|
||||||
|
using DotBased.ASP.Auth.Services;
|
||||||
|
using Microsoft.AspNetCore.Components.Authorization;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace DotBased.ASP.Auth;
|
||||||
|
|
||||||
|
public class BasedAuthBuilder
|
||||||
|
{
|
||||||
|
public BasedAuthBuilder(IServiceCollection services, Action<BasedAuthConfiguration>? configurationAction = null)
|
||||||
|
{
|
||||||
|
_services = services;
|
||||||
|
Configuration = new BasedAuthConfiguration();
|
||||||
|
configurationAction?.Invoke(Configuration);
|
||||||
|
|
||||||
|
services.AddSingleton<BasedAuthConfiguration>(Configuration);
|
||||||
|
services.AddScoped<AuthService>();
|
||||||
|
|
||||||
|
services.AddScoped<AuthenticationStateProvider, BasedServerAuthenticationStateProvider>();
|
||||||
|
services.AddAuthentication(options =>
|
||||||
|
{
|
||||||
|
options.DefaultScheme = BasedAuthenticationHandler.AuthenticationScheme;
|
||||||
|
}).AddScheme<BasedAuthenticationHandlerOptions, BasedAuthenticationHandler>(BasedAuthenticationHandler.AuthenticationScheme, null);
|
||||||
|
services.AddAuthorization();
|
||||||
|
services.AddCascadingAuthenticationState();
|
||||||
|
}
|
||||||
|
public BasedAuthConfiguration Configuration { get; }
|
||||||
|
private readonly IServiceCollection _services;
|
||||||
|
|
||||||
|
public void AddSessionStateProvider<TSessionStateProviderType>() where TSessionStateProviderType : ISessionStateProvider => _services.AddScoped(typeof(ISessionStateProvider), typeof(TSessionStateProviderType));
|
||||||
|
}
|
|
@ -1,3 +1,5 @@
|
||||||
|
using DotBased.ASP.Auth.Services;
|
||||||
|
|
||||||
namespace DotBased.ASP.Auth;
|
namespace DotBased.ASP.Auth;
|
||||||
|
|
||||||
public class BasedAuthConfiguration
|
public class BasedAuthConfiguration
|
||||||
|
|
|
@ -1,24 +0,0 @@
|
||||||
using System.Security.Claims;
|
|
||||||
using DotBased.Logging;
|
|
||||||
using Microsoft.AspNetCore.Components.Authorization;
|
|
||||||
|
|
||||||
namespace DotBased.ASP.Auth;
|
|
||||||
|
|
||||||
// RevalidatingServerAuthenticationStateProvider
|
|
||||||
public class BasedAuthenticationStateProvider : AuthenticationStateProvider
|
|
||||||
{
|
|
||||||
public BasedAuthenticationStateProvider(BasedAuthConfiguration configuration)
|
|
||||||
{
|
|
||||||
_config = configuration;
|
|
||||||
_logger = LogService.RegisterLogger(typeof(BasedAuthenticationStateProvider));
|
|
||||||
}
|
|
||||||
|
|
||||||
private BasedAuthConfiguration _config;
|
|
||||||
private ILogger _logger;
|
|
||||||
private AuthenticationState _anonState = new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>() {new Claim(ClaimTypes.Role, "test")})));
|
|
||||||
|
|
||||||
public override Task<AuthenticationState> GetAuthenticationStateAsync()
|
|
||||||
{
|
|
||||||
return Task.FromResult(_anonState);
|
|
||||||
}
|
|
||||||
}
|
|
29
DotBased.ASP.Auth/BasedServerAuthenticationStateProvider.cs
Normal file
29
DotBased.ASP.Auth/BasedServerAuthenticationStateProvider.cs
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
using System.Security.Claims;
|
||||||
|
using DotBased.Logging;
|
||||||
|
using Microsoft.AspNetCore.Components.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Components.Server;
|
||||||
|
using ILogger = DotBased.Logging.ILogger;
|
||||||
|
|
||||||
|
namespace DotBased.ASP.Auth;
|
||||||
|
|
||||||
|
// RevalidatingServerAuthenticationStateProvider
|
||||||
|
// AuthenticationStateProvider
|
||||||
|
public class BasedServerAuthenticationStateProvider : ServerAuthenticationStateProvider
|
||||||
|
{
|
||||||
|
public BasedServerAuthenticationStateProvider(BasedAuthConfiguration configuration, ISessionStateProvider stateProvider)
|
||||||
|
{
|
||||||
|
_config = configuration;
|
||||||
|
_stateProvider = stateProvider;
|
||||||
|
_logger = LogService.RegisterLogger(typeof(BasedServerAuthenticationStateProvider));
|
||||||
|
}
|
||||||
|
|
||||||
|
private BasedAuthConfiguration _config;
|
||||||
|
private ISessionStateProvider _stateProvider;
|
||||||
|
private ILogger _logger;
|
||||||
|
private readonly AuthenticationState _anonState = new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>() {new Claim(ClaimTypes.Role, "test")})));
|
||||||
|
|
||||||
|
public override Task<AuthenticationState> GetAuthenticationStateAsync()
|
||||||
|
{
|
||||||
|
return Task.FromResult(_anonState);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,3 @@
|
||||||
using DotBased.ASP.Auth.Scheme;
|
|
||||||
using Microsoft.AspNetCore.Components.Authorization;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
namespace DotBased.ASP.Auth;
|
namespace DotBased.ASP.Auth;
|
||||||
|
@ -10,19 +8,11 @@ public static class DotBasedAuthDependencyInjection
|
||||||
/// Use the DotBased authentication implementation
|
/// Use the DotBased authentication implementation
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>Use the app.UseAuthentication() and app.UseAuthorization()!</remarks>
|
/// <remarks>Use the app.UseAuthentication() and app.UseAuthorization()!</remarks>
|
||||||
/// <param name="services">Service colllection</param>
|
/// <param name="services">Service collection</param>
|
||||||
/// <param name="configurationAction">DotBased auth configuration</param>
|
/// <param name="configurationAction">DotBased auth configuration</param>
|
||||||
public static void UseBasedAuth(this IServiceCollection services, Action<BasedAuthConfiguration>? configurationAction = null)
|
public static BasedAuthBuilder UseBasedServerAuth(this IServiceCollection services, Action<BasedAuthConfiguration>? configurationAction = null)
|
||||||
{
|
{
|
||||||
var config = new BasedAuthConfiguration();
|
var authBuilder = new BasedAuthBuilder(services, configurationAction);
|
||||||
configurationAction?.Invoke(config);
|
return authBuilder;
|
||||||
services.AddSingleton<BasedAuthConfiguration>(config);
|
|
||||||
|
|
||||||
services.AddScoped<AuthenticationStateProvider, BasedAuthenticationStateProvider>();
|
|
||||||
services.AddAuthentication(options =>
|
|
||||||
{
|
|
||||||
options.DefaultScheme = BasedAuthenticationHandler.AuthenticationScheme;
|
|
||||||
}).AddScheme<BasedAuthenticationHandlerOptions, BasedAuthenticationHandler>(BasedAuthenticationHandler.AuthenticationScheme, null);
|
|
||||||
services.AddAuthorization();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
using DotBased.ASP.Auth.Domains.Auth;
|
using DotBased.ASP.Auth.Domains.Auth;
|
||||||
using DotBased.ASP.Auth.Domains.Identity;
|
using DotBased.ASP.Auth.Domains.Identity;
|
||||||
|
using DotBased.Objects;
|
||||||
|
|
||||||
namespace DotBased.ASP.Auth;
|
namespace DotBased.ASP.Auth;
|
||||||
|
|
||||||
|
@ -32,7 +33,4 @@ public interface IAuthDataProvider
|
||||||
public Task<Result> UpdateAuthenticationStateAsync(AuthenticationStateModel authenticationState);
|
public Task<Result> UpdateAuthenticationStateAsync(AuthenticationStateModel authenticationState);
|
||||||
public Task<Result> DeleteAuthenticationStateAsync(AuthenticationStateModel authenticationState);
|
public Task<Result> DeleteAuthenticationStateAsync(AuthenticationStateModel authenticationState);
|
||||||
public Task<Result<AuthenticationStateModel>> GetAuthenticationStateAsync(string id);
|
public Task<Result<AuthenticationStateModel>> GetAuthenticationStateAsync(string id);
|
||||||
|
|
||||||
// Authorization
|
|
||||||
|
|
||||||
}
|
}
|
8
DotBased.ASP.Auth/ISessionStateProvider.cs
Normal file
8
DotBased.ASP.Auth/ISessionStateProvider.cs
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
namespace DotBased.ASP.Auth;
|
||||||
|
|
||||||
|
public interface ISessionStateProvider
|
||||||
|
{
|
||||||
|
public const string SessionStateName = "BasedServerSession";
|
||||||
|
public Task<Result<string>> GetSessionStateAsync();
|
||||||
|
public Task<Result> SetSessionStateAsync(string state);
|
||||||
|
}
|
6
DotBased.ASP.Auth/Services/AuthService.cs
Normal file
6
DotBased.ASP.Auth/Services/AuthService.cs
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
namespace DotBased.ASP.Auth.Services;
|
||||||
|
|
||||||
|
public class AuthService
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user