title: Server Side Blazor Asp.Net Identity SignInAsync error : The response headers cannot be modified
body: <p>I'm trying to get the Asp.Net Identity login working through Blazor in the Visual Studio template application it still uses Razor Pages and MVC to login, but can only get it to work on the event <code>OnInitAsync</code>, which is not useful because it needs to be done on a button click and not when the page is loading.</p>
  
  <p>My failing code is </p>
  
  <pre><code>protected async Task LoginTest()
  {
     await _SignInManager.SignInAsync(new ApplicationUser()
     { UserName = "test@test.com" }, true);
     UriHelper.NavigateTo("/", true);
  }
  </code></pre>
  
  <p>I get the error:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Has anyone had any success in getting this working? As I mentioned I can get it to work if I put the above function inside the <code>OnInitAsync</code> method but it's no good doing it there.</p>
  
  <p>Any help would be much appreciated.</p>