我试图让 Asp.Net Identity 登录在 Visual Studio 模板应用程序中通过 Blazor 工作,它仍然使用 Razor Pages 和 MVC 登录,但只能让它在事件上工作OnInitAsync
,这没有用,因为它需要通过单击按钮完成,而不是在页面加载时完成。
我的失败代码是
protected async Task LoginTest()
{
await _SignInManager.SignInAsync(new ApplicationUser()
{ UserName = "test@test.com" }, true);
UriHelper.NavigateTo("/", true);
}
我得到错误:
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.Internal.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 WebApplication3.Pages.Account.Login.RegUser() in C:\Users\david\source\repos\WebApplication3\WebApplication3\Pages\Account\Login.razor:line 28
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
at Microsoft.AspNetCore.Components.Rendering.Renderer.GetErrorHandledTask(Task taskToHandle)
有没有人成功完成这项工作?正如我所提到的,如果我将上述函数放在方法中,我可以让它工作,OnInitAsync
但在那里这样做并不好。
任何帮助将非常感激。