ExamEdit&Check

This commit is contained in:
SpecialX
2025-06-12 19:01:05 +08:00
parent e26881ec2f
commit b77ed0b30f
18 changed files with 729 additions and 328 deletions

View File

@@ -13,21 +13,20 @@ namespace TechHelper.Client.HttpRepository
{
public class AuthenticationClientService : IAuthenticationClientService
{
private HttpClient _client;
private readonly IHttpClientFactory _clientFactory;
private readonly HttpClient _client;
private readonly JsonSerializerOptions _options =
new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
private readonly AuthenticationStateProvider _stateProvider;
private readonly ILocalStorageService _localStorageService;
private readonly NavigationManager _navigationManager;
public AuthenticationClientService(IHttpClientFactory httpClientFactory,
// 构造函数现在直接接收 HttpClient
public AuthenticationClientService(HttpClient client, // <-- 修正点:直接注入 HttpClient
AuthenticationStateProvider authenticationStateProvider,
ILocalStorageService localStorageService,
NavigationManager navigationManager)
{
_clientFactory = httpClientFactory;
//_client = httpClientFactory.CreateClient("Default");
_client = client; // <-- 修正点:直接赋值
_localStorageService = localStorageService;
_stateProvider = authenticationStateProvider;
_navigationManager = navigationManager;
@@ -35,121 +34,105 @@ namespace TechHelper.Client.HttpRepository
public async Task<AuthResponseDto> LoginAsync(UserForAuthenticationDto userForAuthenticationDto)
{
using (_client = _clientFactory.CreateClient("Default"))
{
// 移除 using (_client = _clientFactory.CreateClient("Default"))
// _client 已经是注入的实例,直接使用它
var reponse = await _client.PostAsJsonAsync("account/login",
userForAuthenticationDto);
var reponse = await _client.PostAsJsonAsync("account/login",
userForAuthenticationDto);
var content = await reponse.Content.ReadAsStringAsync();
var content = await reponse.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<AuthResponseDto>(content, _options);
var result = JsonSerializer.Deserialize<AuthResponseDto>(content, _options);
if (!reponse.IsSuccessStatusCode || result.Is2StepVerificationRequired)
return result;
if (!reponse.IsSuccessStatusCode || result.Is2StepVerificationRequired)
return result;
_localStorageService.SetItem("authToken", result.Token);
_localStorageService.SetItem("refreshToken", result.RefreshToken);
((AuthStateProvider)_stateProvider).NotifyUserAuthentication(
result.Token);
_localStorageService.SetItem("authToken", result.Token);
_localStorageService.SetItem("refreshToken", result.RefreshToken);
((AuthStateProvider)_stateProvider).NotifyUserAuthentication(
result.Token);
// 直接在注入的 _client 实例上设置默认请求头
_client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(
"bearer", result.Token);
_client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(
"bearer", result.Token);
return new AuthResponseDto { IsAuthSuccessful = true };
}
return new AuthResponseDto { IsAuthSuccessful = true };
}
public async Task LogoutAsync()
{
using (_client = _clientFactory.CreateClient("Default"))
{
_localStorageService.RemoveItem("authToken");
_localStorageService.RemoveItem("refreshToken");
((AuthStateProvider)_stateProvider).NotifyUserLogout();
// 移除 using (_client = _clientFactory.CreateClient("Default"))
_localStorageService.RemoveItem("authToken");
_localStorageService.RemoveItem("refreshToken");
((AuthStateProvider)_stateProvider).NotifyUserLogout();
_client.DefaultRequestHeaders.Authorization = null;
}
// 直接在注入的 _client 实例上清除默认请求头
_client.DefaultRequestHeaders.Authorization = null;
}
public async Task<string> RefreshTokenAsync()
{
using (_client = _clientFactory.CreateClient("Default"))
{
var token = _localStorageService.GetItem<string>("authToken");
var refreshToken = _localStorageService.GetItem<string>("refreshToken");
// 移除 using (_client = _clientFactory.CreateClient("Default"))
var token = _localStorageService.GetItem<string>("authToken");
var refreshToken = _localStorageService.GetItem<string>("refreshToken");
var response = await _client.PostAsJsonAsync("token/refresh",
new RefreshTokenDto
{
Token = token,
RefreshToken = refreshToken
});
var response = await _client.PostAsJsonAsync("token/refresh",
new RefreshTokenDto
{
Token = token,
RefreshToken = refreshToken
});
var content = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<AuthResponseDto>(content, _options);
var content = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<AuthResponseDto>(content, _options);
_localStorageService.SetItem("authToken", result.Token);
_localStorageService.SetItem("refreshToken", result.RefreshToken);
_localStorageService.SetItem("authToken", result.Token);
_localStorageService.SetItem("refreshToken", result.RefreshToken);
_client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("bearer", result.Token);
// 直接在注入的 _client 实例上设置默认请求头
_client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("bearer", result.Token);
return result.Token;
}
return result.Token;
}
public async Task<ResponseDto> RegisterUserAsync(UserForRegistrationDto userForRegistrationDto)
{
using (_client = _clientFactory.CreateClient("Default"))
// 移除 using (_client = _clientFactory.CreateClient("Default"))
userForRegistrationDto.ClientURI = Path.Combine(
_navigationManager.BaseUri, "emailconfirmation");
var reponse = await _client.PostAsJsonAsync("account/register",
userForRegistrationDto);
if (!reponse.IsSuccessStatusCode)
{
userForRegistrationDto.ClientURI = Path.Combine(
_navigationManager.BaseUri, "emailconfirmation");
var reponse = await _client.PostAsJsonAsync("account/register",
userForRegistrationDto);
if (!reponse.IsSuccessStatusCode)
{
var content = await reponse.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<ResponseDto>(content, _options);
return result;
}
return new ResponseDto { IsSuccessfulRegistration = true };
var content = await reponse.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<ResponseDto>(content, _options);
return result;
}
return new ResponseDto { IsSuccessfulRegistration = true };
}
public async Task<HttpStatusCode> ForgotPasswordAsync(ForgotPasswordDto forgotPasswordDto)
{
using (_client = _clientFactory.CreateClient("Default"))
{
// 移除 using (_client = _clientFactory.CreateClient("Default"))
forgotPasswordDto.ClientURI = Path.Combine(_navigationManager.BaseUri, "resetpassword");
var result = await _client.PostAsJsonAsync("account/forgotpassword",
forgotPasswordDto);
forgotPasswordDto.ClientURI = Path.Combine(_navigationManager.BaseUri, "resetpassword");
var result = await _client.PostAsJsonAsync("account/forgotpassword",
forgotPasswordDto);
return result.StatusCode;
}
return result.StatusCode;
}
public async Task<ResetPasswordResponseDto> ResetPasswordAsync(ResetPasswordDto resetPasswordDto)
{
using (_client = _clientFactory.CreateClient("Default"))
{
var resetresult = await _client.PostAsJsonAsync("account/resetpassword",
resetPasswordDto);
// 移除 using (_client = _clientFactory.CreateClient("Default"))
var resetresult = await _client.PostAsJsonAsync("account/resetpassword",
resetPasswordDto);
var resetContent = await resetresult.Content.ReadAsStringAsync();
var resetContent = await resetresult.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<ResetPasswordResponseDto>(resetContent, _options);
var result = JsonSerializer.Deserialize<ResetPasswordResponseDto>(resetContent, _options);
return result;
}
return result;
}
public async Task<HttpStatusCode> EmailConfirmationAsync(string email, string token)
@@ -159,40 +142,35 @@ namespace TechHelper.Client.HttpRepository
["email"] = email,
["token"] = token
};
using (_client = _clientFactory.CreateClient("Default"))
{
var response = await _client.GetAsync(QueryHelpers.AddQueryString(
"account/emailconfirmation", queryStringParam));
// 移除 using (_client = _clientFactory.CreateClient("Default"))
var response = await _client.GetAsync(QueryHelpers.AddQueryString(
"account/emailconfirmation", queryStringParam));
return response.StatusCode;
}
return response.StatusCode;
}
public async Task<AuthResponseDto> LoginVerfication(TwoFactorVerificationDto twoFactorVerificationDto)
{
using (_client = _clientFactory.CreateClient("Default"))
{
// 移除 using (_client = _clientFactory.CreateClient("Default"))
var reponse = await _client.PostAsJsonAsync("account/twostepverification",
twoFactorVerificationDto);
var reponse = await _client.PostAsJsonAsync("account/twostepverification",
twoFactorVerificationDto);
var content = await reponse.Content.ReadAsStringAsync();
var content = await reponse.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<AuthResponseDto>(content, _options);
var result = JsonSerializer.Deserialize<AuthResponseDto>(content, _options);
if (!reponse.IsSuccessStatusCode)
return result;
if (!reponse.IsSuccessStatusCode)
return result;
_localStorageService.SetItem("authToken", result.Token);
_localStorageService.SetItem("refreshToken", result.RefreshToken);
((AuthStateProvider)_stateProvider).NotifyUserAuthentication(
result.Token);
_localStorageService.SetItem("authToken", result.Token);
_localStorageService.SetItem("refreshToken", result.RefreshToken);
((AuthStateProvider)_stateProvider).NotifyUserAuthentication(
result.Token);
_client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(
"bearer", result.Token);
_client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(
"bearer", result.Token);
return new AuthResponseDto { IsAuthSuccessful = true };
}
return new AuthResponseDto { IsAuthSuccessful = true };
}
}
}
}