标头是只读的,响应已在 Blazor 服务器 SignInManager PasswordSignInAsync 上开始

问题描述 投票:0回答:1

这是我的登录页面:


    @page "/Login"
    
    @inject SignInManager<ApplicationUser> SignInManager
    @inject ILogger<Login> Logger
    @inject NavigationManager NavigationManager
    
    <PageTitle>Se connecter</PageTitle>
    
    <h1>Se connecter</h1>
    
    @if (errorMessage != null)
    {
        <MudAlert Severity="Severity.Error">@errorMessage</MudAlert>
    }
    <MudContainer MaxWidth="MaxWidth.Small">
    <MudCard>
        <EditForm Model="@Input" OnValidSubmit="Submit">
            <MudCardContent>
                <MudTextField @bind-Value="Input.Email"
                              For="@(() => Input.Email)"
                              Immediate="true"
                              Label="Courriel" />

                <MudTextField @bind-Value="Input.Password"
                              For="@(() => Input.Password)"
                              Immediate="true"
                              Label="Mot de passe"
                              InputType="InputType.Password" />
            </MudCardContent>
            <MudCardActions>
                @if (InProgress)
                {
                    <MudProgressCircular Color="Color.Primary" Indeterminate="true" Class="ml-auto" />
                }
                else
                {
                    <MudButton Variant="Variant.Filled" Color="Color.Primary" Class="ml-auto" ButtonType="ButtonType.Submit">Se connecter</MudButton>
                }
            </MudCardActions>
        </EditForm>
    </MudCard>
</MudContainer>
    
    @code {
        private string? errorMessage = null;
        private bool InProgress = false;
    
        [SupplyParameterFromForm]
        private InputModel Input { get; set; } = new();
    
        private async Task Submit()
        {
            InProgress = true;
            // This doesn't count login failures towards account lockout
            // To enable password failures to trigger account lockout, set lockoutOnFailure: true
            var result = await SignInManager.PasswordSignInAsync(Input.Email, Input.Password, false, lockoutOnFailure: true);
            if (result.Succeeded)
            {
                Logger.LogInformation("L'utilisateur est connecté.");
                NavigationManager.NavigateTo("/Home");
            }
            else if (result.IsLockedOut)
            {
                Logger.LogWarning("Le compte a été bloqué.");
                errorMessage = "Le compte a été bloqué.";
            }
            else
            {
                errorMessage = "Erreur: tentative de connexion invalide.";
            }
            InProgress = false;
        }
    
        private sealed class InputModel
        {
            [Required(ErrorMessage = "Le courriel ne doit pas être vide.")]
            [EmailAddress(ErrorMessage = "Ceci n'est pas une adresse courriel valide.")]
            public string Email { get; set; } = "";
    
            [Required(ErrorMessage = "Le mot de passe ne doit pas être vide.")]
            [DataType(DataType.Password)]
            public string Password { get; set; } = "";
        }
    }

当我以用户身份登录时,它正在加载并且永远不会结束加载。所以我设置了一个断点,我注意到调用在这一行停止:

var 结果 = 等待 SignInManager.PasswordSignInAsync(Input.Email, 输入.密码, false, lockoutOnFailure: true);

无限循环。我根本没有收到任何错误消息。只是它永远不会结束加载。我不知道我该怎么做才能让它工作......

这是我在 App.razor 中的路由标签:

我发现我在控制台中遇到了这个错误:

fail: Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost[111]
      Unhandled exception in circuit 'p1W8tSWhUaRGiR9ur-mhK8l0QIyLyyDYpUF8eGrBwRY'.
      System.InvalidOperationException: Headers are read-only, response has already started.
c# asp.net-core authentication asp.net-identity blazor-server-side
1个回答
0
投票

SignInManager.PasswordSignInAsync
实际上需要访问 “httpcontext”工作。但“httpcontext”仅在 SSR 渲染模式下可用。 (https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-context?view=aspnetcore-8.0#ihttpcontextaccessorhttpcontext-in-razor-components-blazor
所以这个页面必须使用SSR。 Editform 也可以在没有“交互”的 SSR 中工作。 (https://learn.microsoft.com/en-us/aspnet/core/blazor/components/render-modes?view=aspnetcore-8.0#static-server-side-rendering-static-ssr
由于您使用的是全局渲染模式,因此您可以修改 App.razor,如下所示:

...
    <Routes @rendermode="RenderModeForPage" />
...
@code {
    [CascadingParameter]
    private HttpContext HttpContext { get; set; } = default!;
//null rendermode represents SSR.
    private IComponentRenderMode? RenderModeForPage => HttpContext.Request.Path.StartsWithSegments("/Login") ? null : InteractiveServer;
}
© www.soinside.com 2019 - 2024. All rights reserved.