* Added Authorization which is not working
This commit is contained in:
@@ -1,29 +1,62 @@
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using Blazored.LocalStorage;
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
using OrdersManagementDataModel.Dtos;
|
||||
|
||||
namespace OrdersManagement;
|
||||
|
||||
public class CustomAuthenticationStateProvider : AuthenticationStateProvider
|
||||
public class CustomAuthenticationStateProvider(ILocalStorageService localStorage) : AuthenticationStateProvider
|
||||
{
|
||||
private UserDto? _currentUser;
|
||||
private string? _token;
|
||||
private ClaimsPrincipal _currentUser = new(new ClaimsIdentity());
|
||||
|
||||
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)));
|
||||
if (string.IsNullOrEmpty(_token))
|
||||
{
|
||||
return Task.FromResult(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity())));
|
||||
}
|
||||
|
||||
var handler = new JwtSecurityTokenHandler();
|
||||
var jwtToken = handler.ReadJwtToken(_token);
|
||||
var identity = new ClaimsIdentity(jwtToken.Claims, "jwt");
|
||||
_currentUser = new ClaimsPrincipal(identity);
|
||||
return Task.FromResult(new AuthenticationState(_currentUser));
|
||||
}
|
||||
|
||||
public Task MarkUserAsAuthenticated(UserDto? user)
|
||||
public async Task MarkUserAsAuthenticated(string? token)
|
||||
{
|
||||
_currentUser = user;
|
||||
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
|
||||
return Task.CompletedTask;
|
||||
_token = token;
|
||||
await localStorage.SetItemAsync("authToken", token);
|
||||
var handler = new JwtSecurityTokenHandler();
|
||||
var jwtToken = handler.ReadJwtToken(token);
|
||||
var identity = new ClaimsIdentity(jwtToken.Claims, "jwt");
|
||||
_currentUser = new ClaimsPrincipal(identity);
|
||||
NotifyAuthenticationStateChanged(Task.FromResult(new AuthenticationState(_currentUser)));
|
||||
}
|
||||
|
||||
public void MarkUserAsLoggedOut()
|
||||
public async Task MarkUserAsLoggedOut()
|
||||
{
|
||||
_currentUser = null;
|
||||
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
|
||||
_token = null;
|
||||
await localStorage.RemoveItemAsync("authToken");
|
||||
_currentUser = new ClaimsPrincipal(new ClaimsIdentity());
|
||||
NotifyAuthenticationStateChanged(Task.FromResult(new AuthenticationState(_currentUser)));
|
||||
}
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
_token = await localStorage.GetItemAsync<string>("authToken");
|
||||
|
||||
if (!string.IsNullOrEmpty(_token))
|
||||
{
|
||||
var handler = new JwtSecurityTokenHandler();
|
||||
var jwtToken = handler.ReadJwtToken(_token);
|
||||
var identity = new ClaimsIdentity(jwtToken.Claims, "jwt");
|
||||
_currentUser = new ClaimsPrincipal(identity);
|
||||
NotifyAuthenticationStateChanged(Task.FromResult(new AuthenticationState(_currentUser)));
|
||||
}
|
||||
}
|
||||
|
||||
public string? GetToken() => _token;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user