Dav*_*ins 10 asp.net-core blazor-server-side asp.net-core-3.0
我试图在 Visual Studio 模板应用程序中通过 Blazor 获取 Asp.Net Identity 登录,它仍然使用 Razor Pages 和 MVC 登录,但只能让它在事件上工作OnInitAsync,这没有用,因为它需要在按钮点击时完成,而不是在页面加载时完成。
我的失败代码是
protected async Task LoginTest()
{
await _SignInManager.SignInAsync(new ApplicationUser()
{ UserName = "test@test.com" }, true);
UriHelper.NavigateTo("/", true);
}
Run Code Online (Sandbox Code Playgroud)
我收到错误:
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)
Run Code Online (Sandbox Code Playgroud)
有没有人成功地完成这项工作?正如我提到的,如果我将上述函数放在OnInitAsync方法中,我可以让它工作,但在那里做并不好。
任何帮助将非常感激。
Dav*_*ins -3
我发现了这个相关的 GitHub 问题http://github.com/aspnet/AspNetCore/issues/11411
我最终使用重定向后获取方法创建了一个解决方案。在帖子中,我使用用户详细信息加密了登录令牌,并将其作为查询字符串中令牌的获取传递到另一个页面,然后我读取加密的令牌并登录用户。
| 归档时间: |
|
| 查看次数: |
4044 次 |
| 最近记录: |