简体   繁体   English

Blazor Webassembly 认证非常慢

[英]Blazor Webassembly Authentication is verry slow

hi I have created a blazor webassembly project, which uses an API to fetch data and authorize.嗨,我创建了一个 blazor webassembly 项目,它使用 API 来获取数据和授权。 The authentication works fine, the problem is the app is very slow.身份验证工作正常,问题是应用程序非常慢。 For example to login procedure can take up to 5 seconds through the bazor app, while procedure is verry fast if I call the login action through the API.例如,通过 bazor 应用程序登录过程最多可能需要 5 秒,而如果我通过 API 调用登录操作,则过程非常快。 What am I doing wrong?我究竟做错了什么? Here is the blazor code for the authentication.这是用于身份验证的 blazor 代码。 I have not included the API code since the authentication is fast thorugh postman我没有包含 API 代码,因为身份验证通过 postman 快速完成

Login.razor登录.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 -> Login-function 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;

        }

Thank you!谢谢!

This probably is not your issue, but I ran into the same thing recently.这可能不是你的问题,但我最近遇到了同样的事情。 My authentication time in Blazor WebAssembly jumped from less a second to several seconds.我在 Blazor WebAssembly 中的身份验证时间从不到一秒跳到了几秒。 I tracked it down to denying x-frames.我追查到否认 x 帧。

I think this is only going to affect a hosted Blazor WebAssembly application that is using ASP.NET Identity for Authentication.认为这只会影响使用 ASP.NET 身份验证的托管 Blazor WebAssembly 应用程序。 In other words, anyone that checked Individual Accounts when setting up a hosted project.换句话说,任何在设置托管项目时检查个人帐户的人。

As soon as I setup NWebSpec and added this line it became very slow:一旦我设置了 NWebSpec 并添加了这一行,它就变得非常慢:

app.UseXfo(xfo => xfo.Deny());

I resolved the issue by switching to same origin:我通过切换到相同的来源解决了这个问题:

app.UseXfo(xfo => xfo.SameOrigin());

Leaving this here in case anyone else stumbles on this issue.把这个留在这里,以防其他人偶然发现这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM