mirror of
https://github.com/hmaxnl/DotBased.git
synced 2025-01-18 18:14:20 +01:00
Added custom scheme
This commit is contained in:
parent
de656cc2e8
commit
5341179e94
|
@ -1,20 +1,24 @@
|
||||||
|
using System.Security.Claims;
|
||||||
using DotBased.Logging;
|
using DotBased.Logging;
|
||||||
using Microsoft.AspNetCore.Components.Authorization;
|
using Microsoft.AspNetCore.Components.Authorization;
|
||||||
|
|
||||||
namespace DotBased.ASP.Auth;
|
namespace DotBased.ASP.Auth;
|
||||||
|
|
||||||
|
// RevalidatingServerAuthenticationStateProvider
|
||||||
public class BasedAuthenticationStateProvider : AuthenticationStateProvider
|
public class BasedAuthenticationStateProvider : AuthenticationStateProvider
|
||||||
{
|
{
|
||||||
public BasedAuthenticationStateProvider()
|
public BasedAuthenticationStateProvider(BasedAuthConfiguration configuration)
|
||||||
{
|
{
|
||||||
|
_config = configuration;
|
||||||
_logger = LogService.RegisterLogger(typeof(BasedAuthenticationStateProvider));
|
_logger = LogService.RegisterLogger(typeof(BasedAuthenticationStateProvider));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private BasedAuthConfiguration _config;
|
||||||
private ILogger _logger;
|
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()
|
public override Task<AuthenticationState> GetAuthenticationStateAsync()
|
||||||
{
|
{
|
||||||
|
return Task.FromResult(_anonState);
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,13 +0,0 @@
|
||||||
using Microsoft.AspNetCore.Builder;
|
|
||||||
using Microsoft.AspNetCore.Components.Authorization;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
|
|
||||||
namespace DotBased.ASP.Auth;
|
|
||||||
|
|
||||||
public static class DotBasedASPAuth
|
|
||||||
{
|
|
||||||
public static void UseBasedAuth(this WebApplicationBuilder builder, BasedAuthConfiguration configuration)
|
|
||||||
{
|
|
||||||
builder.Services.AddScoped<AuthenticationStateProvider, BasedAuthenticationStateProvider>();
|
|
||||||
}
|
|
||||||
}
|
|
28
DotBased.ASP.Auth/DotBasedAuthDependencyInjection.cs
Normal file
28
DotBased.ASP.Auth/DotBasedAuthDependencyInjection.cs
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
31
DotBased.ASP.Auth/Scheme/BasedAuthenticationHandler.cs
Normal file
31
DotBased.ASP.Auth/Scheme/BasedAuthenticationHandler.cs
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
using System.Security.Claims;
|
||||||
|
using System.Text.Encodings.Web;
|
||||||
|
using Microsoft.AspNetCore.Authentication;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
|
namespace DotBased.ASP.Auth.Scheme;
|
||||||
|
|
||||||
|
public class BasedAuthenticationHandler : AuthenticationHandler<BasedAuthenticationHandlerOptions>
|
||||||
|
{
|
||||||
|
public const string AuthenticationScheme = "DotBasedAuthentication";
|
||||||
|
|
||||||
|
#pragma warning disable CS0618 // Type or member is obsolete
|
||||||
|
public BasedAuthenticationHandler(IOptionsMonitor<BasedAuthenticationHandlerOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock)
|
||||||
|
#pragma warning restore CS0618 // Type or member is obsolete
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public BasedAuthenticationHandler(IOptionsMonitor<BasedAuthenticationHandlerOptions> options, ILoggerFactory logger, UrlEncoder encoder) : base(options, logger, encoder)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
|
||||||
|
{
|
||||||
|
var principal = new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>() { new Claim(ClaimTypes.Role, "Admin"), new Claim(ClaimTypes.Name, "Anon") }, AuthenticationScheme));
|
||||||
|
var ticket = new AuthenticationTicket(principal, AuthenticationScheme);
|
||||||
|
return Task.FromResult(AuthenticateResult.Success(ticket));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
using Microsoft.AspNetCore.Authentication;
|
||||||
|
|
||||||
|
namespace DotBased.ASP.Auth.Scheme;
|
||||||
|
|
||||||
|
public class BasedAuthenticationHandlerOptions : AuthenticationSchemeOptions
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user