嗨,我已经创建了一个出色的webassembly项目,该项目使用API来获取数据并进行授权。身份验证工作正常,问题在于应用程序速度很慢。例如,通过bazor应用程序登录过程最多可能需要5秒钟,而如果我通过API调用登录操作,则过程非常快。我究竟做错了什么?这是用于身份验证的炫耀代码。由于身份验证是快速的邮递员,因此我未包含API代码
Login.razor
@page "/login"
@inject IAuthService AuthService
@inject NavigationManager NavigationManager
@using System.Net.Http
@inject HttpClient Http
<h1>Register</h1>
<div class="card">
<div class="card-body">
<h5 class="card-title">Please enter your details</h5>
<EditForm Model="UserLoginViewModel" OnValidSubmit="HandleRegistration">
<DataAnnotationsValidator />
<ValidationSummary />
<div class="form-group">
<label for="email">Email address</label>
<InputText Id="email" class="form-control" @bind-Value="UserLoginViewModel.Email" />
<ValidationMessage For="@(() => UserLoginViewModel.Email)" />
</div>
<div class="form-group">
<label for="password">Password</label>
<InputText Id="password" type="password" class="form-control" @bind-Value="UserLoginViewModel.Password" />
<ValidationMessage For="@(() => UserLoginViewModel.Password)" />
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</EditForm>
</div>
</div>
<h2 value="@test" innderHtml="@test" text="@test"></h2>
@code {
string test = "wow";
private UserLoginViewModel UserLoginViewModel = new UserLoginViewModel();
private async Task HandleRegistration()
{
var result = await AuthService.Login(UserLoginViewModel);
if (result.Successful)
{
NavigationManager.NavigateTo("/");
}
}
}
Authservice->登录功能
public async Task<LoginResult> Login(UserLoginViewModel model)
{
var loginAsJson = JsonSerializer.Serialize(model);
var response = await _httpClient.PostAsync("Account/Login", new StringContent(loginAsJson, Encoding.UTF8, "application/json"));
LoginResult loginResult = JsonSerializer.Deserialize<LoginResult>(await response.Content.ReadAsStringAsync(), new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
if (!response.IsSuccessStatusCode)
return loginResult;
await _localStorage.SetItemAsync("authToken", loginResult.Token);
((ApiAuthenticationStateProvider)_authenticationStateProvider).MarkUserAsAuthenticated(model.Email);
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", loginResult.Token);
return loginResult;
}
谢谢!