Reworking auth

This commit is contained in:
max 2024-10-14 15:28:43 +02:00
parent 17f69824eb
commit d98634d888
2 changed files with 9 additions and 3 deletions

View File

@ -1,4 +1,5 @@
using System.Security.Claims; using System.Security.Claims;
using DotBased.ASP.Auth.Scheme;
using DotBased.Logging; using DotBased.Logging;
using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Server; using Microsoft.AspNetCore.Components.Server;
@ -8,6 +9,7 @@ namespace DotBased.ASP.Auth;
// RevalidatingServerAuthenticationStateProvider // RevalidatingServerAuthenticationStateProvider
// AuthenticationStateProvider // AuthenticationStateProvider
// Handles roles
public class BasedServerAuthenticationStateProvider : ServerAuthenticationStateProvider public class BasedServerAuthenticationStateProvider : ServerAuthenticationStateProvider
{ {
public BasedServerAuthenticationStateProvider(BasedAuthConfiguration configuration, ISessionStateProvider stateProvider) public BasedServerAuthenticationStateProvider(BasedAuthConfiguration configuration, ISessionStateProvider stateProvider)
@ -20,10 +22,12 @@ public class BasedServerAuthenticationStateProvider : ServerAuthenticationStateP
private BasedAuthConfiguration _config; private BasedAuthConfiguration _config;
private ISessionStateProvider _stateProvider; private ISessionStateProvider _stateProvider;
private ILogger _logger; private ILogger _logger;
private readonly AuthenticationState _anonState = new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>() {new Claim(ClaimTypes.Role, "test")}))); private readonly AuthenticationState _loggedInState = new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>() { new Claim(ClaimTypes.Role, "Admin"), new Claim(ClaimTypes.Name, "Anon") }, BasedAuthenticationHandler.AuthenticationScheme)));
private readonly AuthenticationState _anonState = new AuthenticationState(new ClaimsPrincipal());
public override Task<AuthenticationState> GetAuthenticationStateAsync() public override Task<AuthenticationState> GetAuthenticationStateAsync()
{ {
return Task.FromResult(_anonState); return Task.FromResult(_loggedInState);
} }
} }

View File

@ -6,6 +6,7 @@ using Microsoft.Extensions.Options;
namespace DotBased.ASP.Auth.Scheme; namespace DotBased.ASP.Auth.Scheme;
// Handles if a user is logged in
public class BasedAuthenticationHandler : AuthenticationHandler<BasedAuthenticationHandlerOptions> public class BasedAuthenticationHandler : AuthenticationHandler<BasedAuthenticationHandlerOptions>
{ {
public const string AuthenticationScheme = "DotBasedAuthentication"; public const string AuthenticationScheme = "DotBasedAuthentication";
@ -24,9 +25,10 @@ public class BasedAuthenticationHandler : AuthenticationHandler<BasedAuthenticat
protected override Task<AuthenticateResult> HandleAuthenticateAsync() protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{ {
/*var principal = new ClaimsPrincipal();*/ /*var principal = new ClaimsPrincipal(new ClaimsIdentity());*/
var principal = new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>() { new Claim(ClaimTypes.Role, "Admin"), new Claim(ClaimTypes.Name, "Anon") }, AuthenticationScheme)); 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); var ticket = new AuthenticationTicket(principal, AuthenticationScheme);
return Task.FromResult(AuthenticateResult.Success(ticket)); return Task.FromResult(AuthenticateResult.Success(ticket));
/*return AuthenticateResult.Fail("No login found!");*/
} }
} }