From d98634d8887e0bab7add7f2181c2cdd5db77e1d2 Mon Sep 17 00:00:00 2001 From: max Date: Mon, 14 Oct 2024 15:28:43 +0200 Subject: [PATCH] Reworking auth --- .../BasedServerAuthenticationStateProvider.cs | 8 ++++++-- DotBased.ASP.Auth/Scheme/BasedAuthenticationHandler.cs | 4 +++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/DotBased.ASP.Auth/BasedServerAuthenticationStateProvider.cs b/DotBased.ASP.Auth/BasedServerAuthenticationStateProvider.cs index 579019c..f64a643 100644 --- a/DotBased.ASP.Auth/BasedServerAuthenticationStateProvider.cs +++ b/DotBased.ASP.Auth/BasedServerAuthenticationStateProvider.cs @@ -1,4 +1,5 @@ using System.Security.Claims; +using DotBased.ASP.Auth.Scheme; using DotBased.Logging; using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Components.Server; @@ -8,6 +9,7 @@ namespace DotBased.ASP.Auth; // RevalidatingServerAuthenticationStateProvider // AuthenticationStateProvider +// Handles roles public class BasedServerAuthenticationStateProvider : ServerAuthenticationStateProvider { public BasedServerAuthenticationStateProvider(BasedAuthConfiguration configuration, ISessionStateProvider stateProvider) @@ -20,10 +22,12 @@ public class BasedServerAuthenticationStateProvider : ServerAuthenticationStateP private BasedAuthConfiguration _config; private ISessionStateProvider _stateProvider; private ILogger _logger; - private readonly AuthenticationState _anonState = new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity(new List() {new Claim(ClaimTypes.Role, "test")}))); + private readonly AuthenticationState _loggedInState = new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity(new List() { new Claim(ClaimTypes.Role, "Admin"), new Claim(ClaimTypes.Name, "Anon") }, BasedAuthenticationHandler.AuthenticationScheme))); + private readonly AuthenticationState _anonState = new AuthenticationState(new ClaimsPrincipal()); + public override Task GetAuthenticationStateAsync() { - return Task.FromResult(_anonState); + return Task.FromResult(_loggedInState); } } \ No newline at end of file diff --git a/DotBased.ASP.Auth/Scheme/BasedAuthenticationHandler.cs b/DotBased.ASP.Auth/Scheme/BasedAuthenticationHandler.cs index 2f7678a..aaca335 100644 --- a/DotBased.ASP.Auth/Scheme/BasedAuthenticationHandler.cs +++ b/DotBased.ASP.Auth/Scheme/BasedAuthenticationHandler.cs @@ -6,6 +6,7 @@ using Microsoft.Extensions.Options; namespace DotBased.ASP.Auth.Scheme; +// Handles if a user is logged in public class BasedAuthenticationHandler : AuthenticationHandler { public const string AuthenticationScheme = "DotBasedAuthentication"; @@ -24,9 +25,10 @@ public class BasedAuthenticationHandler : AuthenticationHandler HandleAuthenticateAsync() { - /*var principal = new ClaimsPrincipal();*/ + /*var principal = new ClaimsPrincipal(new ClaimsIdentity());*/ var principal = new ClaimsPrincipal(new ClaimsIdentity(new List() { new Claim(ClaimTypes.Role, "Admin"), new Claim(ClaimTypes.Name, "Anon") }, AuthenticationScheme)); var ticket = new AuthenticationTicket(principal, AuthenticationScheme); return Task.FromResult(AuthenticateResult.Success(ticket)); + /*return AuthenticateResult.Fail("No login found!");*/ } } \ No newline at end of file