0

我正在开发一个 Blazor 服务器端项目,我目前正在为其创建自定义登录页面。由于我对 Web 应用程序了解不多(这就是我使用 blazor 的原因),所以在 stackoverflow/github 上发布的可能解决方案到目前为止还没有为我工作。

直截了当:我希望内置SignInManager处理用户提供的凭据的验证。这些凭据被设置为FormLoginUser类实例的属性。

公共类 FormLoginUser
{
    [必填,DataType(DataType.EmailAddress)]
    公共字符串电子邮件{获取;放; }

    [必需的]
    公共字符串密码{获取;放; }
}

登录页面背后的代码如下:

公共类 LoginBase : ComponentBase
{
    [注入]
    私人用户管理器<应用程序用户>用户管理器{得到;放; }

    [注入]
    私人 SignInManager<ApplicationUser> signInManager { 获取;放; }

    [注入]
    私人 NavigationManager NavigationManager { 获取;放; }

    受保护的 FormLoginUser 用户 { 获取;放; }

    受保护的布尔 IsLoginFailed { 获取;放; }

    受保护的覆盖无效 OnInitialized()
    {
        base.OnInitialized();
        IsLoginFailed = 假;
        用户 = 新的 FormLoginUser();
    }

    受保护的异步任务 ValidateUser()
    {
        SignInResult 结果 = 等待 signInManager.PasswordSignInAsync(
            用户名:用户.电子邮件,
            密码:用户密码,
            是持久的:真,
            lockoutOnFailure: false);
        如果(结果。成功)
            NavigationManager.NavigateTo("/");
        别的
            IsLoginFailed = true;
        应该渲染();
    }
}

当用户提交登录表单时,该方法ValidateUser()被调用,此时该User属性将被表单中的数据填充。方法的第一行马上就出错了ValidateUser()

这是抛出的异常:

Microsoft.AspNetCore.Components.Server.Circuits.RemoteRenderer: Warning: Unhandled exception rendering component: The response headers cannot be modified because the response has already started.

System.InvalidOperationException: The response headers cannot be modified because the response has already started.
   at Microsoft.AspNetCore.HttpSys.Internal.HeaderCollection.ThrowIfReadOnly()
   at Microsoft.AspNetCore.HttpSys.Internal.HeaderCollection.set_Item(String key, StringValues value)
   at Microsoft.AspNetCore.Http.ResponseCookies.Append(String key, String value, CookieOptions options)
   at Microsoft.AspNetCore.Authentication.Cookies.ChunkingCookieManager.AppendResponseCookie(HttpContext context, String key, String value, CookieOptions options)
   at Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler.HandleSignInAsync(ClaimsPrincipal user, AuthenticationProperties properties)
   at Microsoft.AspNetCore.Authentication.AuthenticationService.SignInAsync(HttpContext context, String scheme, ClaimsPrincipal principal, AuthenticationProperties properties)
   at Microsoft.AspNetCore.Identity.SignInManager`1.SignInWithClaimsAsync(TUser user, AuthenticationProperties authenticationProperties, IEnumerable`1 additionalClaims)
   at Microsoft.AspNetCore.Identity.SignInManager`1.SignInOrTwoFactorAsync(TUser user, Boolean isPersistent, String loginProvider, Boolean bypassTwoFactor)
   at Microsoft.AspNetCore.Identity.SignInManager`1.PasswordSignInAsync(TUser user, String password, Boolean isPersistent, Boolean lockoutOnFailure)
   at Microsoft.AspNetCore.Identity.SignInManager`1.PasswordSignInAsync(String userName, String password, Boolean isPersistent, Boolean lockoutOnFailure)
   at EvaluationPortal.Pages.Authentication.LoginBase.ValidateUser() in C:\dev\EvaluationPortal\Src\EvaluationPortal\Pages\Authentication\Login.razor.cs:line 33
   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
   at Microsoft.AspNetCore.Components.Forms.EditForm.HandleSubmitAsync()
   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)
Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost: Error: Unhandled exception in circuit 'j2FiQ7xmyQgNWl9lQDS5yHURzyFSUDlJyCeOu33qd-8'.

System.InvalidOperationException: The response headers cannot be modified because the response has already started.
   at Microsoft.AspNetCore.HttpSys.Internal.HeaderCollection.ThrowIfReadOnly()
   at Microsoft.AspNetCore.HttpSys.Internal.HeaderCollection.set_Item(String key, StringValues value)
   at Microsoft.AspNetCore.Http.ResponseCookies.Append(String key, String value, CookieOptions options)
   at Microsoft.AspNetCore.Authentication.Cookies.ChunkingCookieManager.AppendResponseCookie(HttpContext context, String key, String value, CookieOptions options)
   at Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler.HandleSignInAsync(ClaimsPrincipal user, AuthenticationProperties properties)
   at Microsoft.AspNetCore.Authentication.AuthenticationService.SignInAsync(HttpContext context, String scheme, ClaimsPrincipal principal, AuthenticationProperties properties)
   at Microsoft.AspNetCore.Identity.SignInManager`1.SignInWithClaimsAsync(TUser user, AuthenticationProperties authenticationProperties, IEnumerable`1 additionalClaims)
   at Microsoft.AspNetCore.Identity.SignInManager`1.SignInOrTwoFactorAsync(TUser user, Boolean isPersistent, String loginProvider, Boolean bypassTwoFactor)
   at Microsoft.AspNetCore.Identity.SignInManager`1.PasswordSignInAsync(TUser user, String password, Boolean isPersistent, Boolean lockoutOnFailure)
   at Microsoft.AspNetCore.Identity.SignInManager`1.PasswordSignInAsync(String userName, String password, Boolean isPersistent, Boolean lockoutOnFailure)
   at EvaluationPortal.Pages.Authentication.LoginBase.ValidateUser() in C:\dev\EvaluationPortal\Src\EvaluationPortal\Pages\Authentication\Login.razor.cs:line 33
   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
   at Microsoft.AspNetCore.Components.Forms.EditForm.HandleSubmitAsync()
   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)

有没有人知道可能导致这种情况的任何线索,我一直在寻找整个互联网,但似乎找不到任何可以为我解决这个问题的方法。请帮忙。

4

1 回答 1

0

我在 Blazor 身份验证方面也遇到了很多问题,但本教程确实帮助了我。

我相信他实现了与您尝试使用的身份验证相同的身份验证(Microsoft Identity)。

于 2020-06-28T15:38:17.767 回答