I am working on a Blazor server-side project for which I am currently creating a customized sign in page. Since I don't know that much about web apps (which is why I'm using blazor) possible solutions posted on the stackoverflow/github have not worked for me so far.
To the point: I want the built-in SignInManager to handle the validation of the credentials provided by the user. These credentials are set as properties of a FormLoginUser class instance.
public class FormLoginUser
{
[Required, DataType(DataType.EmailAddress)]
public string Email { get; set; }
[Required]
public string Password { get; set; }
}
The code behind for the login page is as follows:
public class LoginBase : ComponentBase
{
[Inject]
private UserManager<ApplicationUser> userManager { get; set; }
[Inject]
private SignInManager<ApplicationUser> signInManager { get; set; }
[Inject]
private NavigationManager NavigationManager { get; set; }
protected FormLoginUser User { get; set; }
protected bool IsLoginFailed { get; set; }
protected override void OnInitialized()
{
base.OnInitialized();
IsLoginFailed = false;
User = new FormLoginUser();
}
protected async Task ValidateUser()
{
SignInResult result = await signInManager.PasswordSignInAsync(
userName: User.Email,
password: User.Password,
isPersistent: true,
lockoutOnFailure: false);
if (result.Succeeded)
NavigationManager.NavigateTo("/");
else
IsLoginFailed = true;
ShouldRender();
}
}
When the user submits the login form the method ValidateUser() is called, by which time the User property will have been filled with data from the form.
Things go wrong of right away on the first line of the ValidateUser() method.
This is the the exception that is thrown:
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)
Has anyone got any clue as to what might cause this, I've been looking all over the internet but cannot seem to find anyting that solves this issue for me. Please help.