* Added Authentication
This commit is contained in:
29
OrdersManagement/CustomAuthenticationStateProvider.cs
Normal file
29
OrdersManagement/CustomAuthenticationStateProvider.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
using OrdersManagementDataModel.Dtos;
|
||||
|
||||
namespace OrdersManagement;
|
||||
|
||||
public class CustomAuthenticationStateProvider : AuthenticationStateProvider
|
||||
{
|
||||
private UserDto? _currentUser;
|
||||
|
||||
public override Task<AuthenticationState> GetAuthenticationStateAsync()
|
||||
{
|
||||
var identity = _currentUser != null ? new ClaimsIdentity([new Claim(ClaimTypes.Name, _currentUser.Login)], "CustomAuth") : new ClaimsIdentity();
|
||||
return Task.FromResult(new AuthenticationState(new ClaimsPrincipal(identity)));
|
||||
}
|
||||
|
||||
public Task MarkUserAsAuthenticated(UserDto? user)
|
||||
{
|
||||
_currentUser = user;
|
||||
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public void MarkUserAsLoggedOut()
|
||||
{
|
||||
_currentUser = null;
|
||||
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user