添加项目文件。
This commit is contained in:
64
TechHelper.Client/AuthProviders/AuthStateProvider.cs
Normal file
64
TechHelper.Client/AuthProviders/AuthStateProvider.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
using System.Security.Claims;
|
||||
using System.Net.Http.Headers;
|
||||
using TechHelper.Features;
|
||||
using Microsoft.JSInterop;
|
||||
|
||||
namespace TechHelper.Client.AuthProviders
|
||||
{
|
||||
public class AuthStateProvider : AuthenticationStateProvider
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly AuthenticationState _anonymous;
|
||||
private readonly ILocalStorageService _localStorageService;
|
||||
|
||||
public AuthStateProvider(ILocalStorageService localStorageService, HttpClient httpClient)
|
||||
{
|
||||
_localStorageService = localStorageService;
|
||||
_httpClient = httpClient;
|
||||
_anonymous = new AuthenticationState(
|
||||
new ClaimsPrincipal(new ClaimsIdentity()));
|
||||
}
|
||||
|
||||
|
||||
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
|
||||
{
|
||||
string? token = null;
|
||||
|
||||
try
|
||||
{
|
||||
token = _localStorageService.GetItem<string>("authToken");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error accessing LocalStorage or parsing token: {ex.Message}");
|
||||
return _anonymous;
|
||||
}
|
||||
if (string.IsNullOrEmpty(token))
|
||||
return _anonymous;
|
||||
|
||||
_httpClient.DefaultRequestHeaders.Authorization =
|
||||
new AuthenticationHeaderValue("bearer", token);
|
||||
|
||||
return new AuthenticationState(new ClaimsPrincipal(
|
||||
new ClaimsIdentity(JWTParser.ParseClaimsFromJwt(token), "jwtAuthType")));
|
||||
}
|
||||
|
||||
public void NotifyUserAuthentication(string token)
|
||||
{
|
||||
var authenticatedUser = new ClaimsPrincipal(new ClaimsIdentity(
|
||||
JWTParser.ParseClaimsFromJwt(token), "jwtAuthType"));
|
||||
|
||||
var authState = Task.FromResult(new AuthenticationState(authenticatedUser));
|
||||
|
||||
NotifyAuthenticationStateChanged(authState);
|
||||
}
|
||||
|
||||
public void NotifyUserLogout()
|
||||
{
|
||||
var authState = Task.FromResult(_anonymous);
|
||||
|
||||
NotifyAuthenticationStateChanged(authState);
|
||||
}
|
||||
}
|
||||
}
|
23
TechHelper.Client/AuthProviders/TestAuthStateProvider.cs
Normal file
23
TechHelper.Client/AuthProviders/TestAuthStateProvider.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace TechHelper.Client.AuthProviders
|
||||
{
|
||||
public class TestAuthStateProvider : AuthenticationStateProvider
|
||||
{
|
||||
|
||||
|
||||
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
|
||||
{
|
||||
var claims = new List<Claim>
|
||||
{
|
||||
new Claim(ClaimTypes.Name, "John Doe"),
|
||||
new Claim(ClaimTypes.Role, "Administrator")
|
||||
};
|
||||
|
||||
var anonymous = new ClaimsIdentity();
|
||||
|
||||
return await Task.FromResult(new AuthenticationState(new ClaimsPrincipal(anonymous)));
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user