This commit is contained in:
SpecialX
2025-06-25 17:21:29 +08:00
parent f9ff57ff72
commit 262e7d6396
21 changed files with 1491 additions and 152 deletions

View File

@@ -20,13 +20,12 @@ namespace TechHelper.Client.HttpRepository
private readonly ILocalStorageService _localStorageService;
private readonly NavigationManager _navigationManager;
// 构造函数现在直接接收 HttpClient
public AuthenticationClientService(HttpClient client, // <-- 修正点:直接注入 HttpClient
public AuthenticationClientService(HttpClient client,
AuthenticationStateProvider authenticationStateProvider,
ILocalStorageService localStorageService,
NavigationManager navigationManager)
{
_client = client; // <-- 修正点:直接赋值
_client = client;
_localStorageService = localStorageService;
_stateProvider = authenticationStateProvider;
_navigationManager = navigationManager;
@@ -34,8 +33,6 @@ namespace TechHelper.Client.HttpRepository
public async Task<AuthResponseDto> LoginAsync(UserForAuthenticationDto userForAuthenticationDto)
{
// 移除 using (_client = _clientFactory.CreateClient("Default"))
// _client 已经是注入的实例,直接使用它
var reponse = await _client.PostAsJsonAsync("account/login",
userForAuthenticationDto);
@@ -71,7 +68,6 @@ namespace TechHelper.Client.HttpRepository
public async Task<string> RefreshTokenAsync()
{
// 移除 using (_client = _clientFactory.CreateClient("Default"))
var token = _localStorageService.GetItem<string>("authToken");
var refreshToken = _localStorageService.GetItem<string>("refreshToken");
@@ -167,6 +163,9 @@ namespace TechHelper.Client.HttpRepository
((AuthStateProvider)_stateProvider).NotifyUserAuthentication(
result.Token);
_client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(
"bearer", result.Token);

View File

@@ -24,16 +24,15 @@ namespace TechHelper.Client.HttpRepository
var authState = await _authenticationStateProvider.Value.GetAuthenticationStateAsync();
var user = authState.User;
// 如果 user 或 claims 为空,表示用户未认证,直接返回空字符串
if (user?.Identity == null || !user.Identity.IsAuthenticated)
{
return string.Empty;
}
var expClaim = user.FindFirst(c => c.Type.Equals("exp"))?.Value; // 使用 ?. 防止空引用
var expClaim = user.FindFirst(c => c.Type.Equals("exp"))?.Value;
if (string.IsNullOrEmpty(expClaim))
{
return string.Empty; // 没有过期时间声明,也直接返回
return string.Empty;
}
var expTime = DateTimeOffset.FromUnixTimeSeconds(
@@ -41,9 +40,11 @@ namespace TechHelper.Client.HttpRepository
var diff = expTime - DateTime.UtcNow;
// 只有当令牌即将过期时才尝试刷新
var n = DateTime.UtcNow;
if (diff.TotalMinutes <= 2)
return await _authenticationClientService.Value.RefreshTokenAsync(); // 访问 .Value 来调用方法
return await _authenticationClientService.Value.RefreshTokenAsync();
return string.Empty;
}

View File

@@ -1,46 +0,0 @@
using Microsoft.AspNetCore.Components.Authorization;
namespace TechHelper.Client.HttpRepository
{
public class RefreshTokenService2
{
private readonly Lazy<AuthenticationStateProvider> _authenticationStateProvider;
private readonly Lazy<IAuthenticationClientService> _authenticationClientService;
public RefreshTokenService2(IServiceProvider serviceProvider)
{
_authenticationStateProvider = new Lazy<AuthenticationStateProvider>(
() => serviceProvider.GetRequiredService<AuthenticationStateProvider>());
_authenticationClientService = new Lazy<IAuthenticationClientService>(
() => serviceProvider.GetRequiredService<IAuthenticationClientService>());
}
public async Task<string> TryRefreshToken()
{
var authState = await _authenticationStateProvider.Value.GetAuthenticationStateAsync();
var user = authState.User;
if (user?.Identity == null || !user.Identity.IsAuthenticated)
{
return string.Empty;
}
var expClaim = user.FindFirst(c => c.Type.Equals("exp"))?.Value;
if (string.IsNullOrEmpty(expClaim))
{
return string.Empty;
}
var expTime = DateTimeOffset.FromUnixTimeSeconds(
Convert.ToInt64(expClaim));
var diff = expTime - DateTime.UtcNow;
if (diff.TotalMinutes <= 2)
return await _authenticationClientService.Value.RefreshTokenAsync();
return string.Empty;
}
}
}