Auth System
This commit is contained in:
79
FoodsharingSiegen.Server/Auth/TokenAuthStateProvider.cs
Normal file
79
FoodsharingSiegen.Server/Auth/TokenAuthStateProvider.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using System.Security.Claims;
|
||||
using FoodsharingSiegen.Contracts;
|
||||
using FoodsharingSiegen.Server.Auth;
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
|
||||
namespace FoodsharingSiegen.Server.Service
|
||||
{
|
||||
/// <summary>
|
||||
/// The token auth state provider class (a. beging, 02.04.2022)
|
||||
/// </summary>
|
||||
/// <seealso cref="AuthenticationStateProvider"/>
|
||||
public class TokenAuthStateProvider : AuthenticationStateProvider
|
||||
{
|
||||
#region Private Fields
|
||||
|
||||
/// <summary> LocalStorageService </summary>
|
||||
private readonly LocalStorageService _localStorageService;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Setup/Teardown
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
/// <param name="localStorageService"></param>
|
||||
public TokenAuthStateProvider(LocalStorageService localStorageService) => _localStorageService = localStorageService;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Override GetAuthenticationStateAsync
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// <summary> Get the current authenticationstate </summary>
|
||||
/// <remarks> A. Beging, 02.02.2022. </remarks>
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
|
||||
{
|
||||
var token = await _localStorageService.GetItem<string>(StorageKeys.TokenKey);
|
||||
var tokenValid = await AuthHelper.ValidateToken(token);
|
||||
|
||||
var identity = new ClaimsIdentity();
|
||||
if (tokenValid)
|
||||
identity = new ClaimsIdentity(new[]
|
||||
{
|
||||
new Claim(ClaimTypes.Name, "user")
|
||||
}, "TODO");
|
||||
|
||||
var claimsPrincipal = new ClaimsPrincipal(identity);
|
||||
return new AuthenticationState(claimsPrincipal);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Method MarkUserAsAuthenticated
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/// <summary> Mark user as authenticated. </summary>
|
||||
/// <remarks> A. Beging, 02.02.2022. </remarks>
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
public void MarkUserAsAuthenticated() => NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Method MarkUserAsLoggedOut
|
||||
|
||||
/// <summary>
|
||||
/// Marks the user as logged out (a. beging, 02.04.2022)
|
||||
/// </summary>
|
||||
public void MarkUserAsLoggedOut()
|
||||
{
|
||||
var anonymousUser = new ClaimsPrincipal(new ClaimsIdentity());
|
||||
var authState = Task.FromResult(new AuthenticationState(anonymousUser));
|
||||
NotifyAuthenticationStateChanged(authState);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user