title: When using SignInManager.SignInAsync(.......) under Blazor on server side then it is throwing the exception
body: <p>I am learning Server side blazor and trying to learn Authentication. Whenever I use SignInManager.SignInAsync(.......), it throws the following exception:</p>
  
  <blockquote>
    <p>System.InvalidOperationException: <strong>The response headers cannot be
    modified because the response has already started.</strong>    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)    <strong>at
    Microsoft.AspNetCore.Authentication.Cookies.ChunkingCookieManager.AppendResponseCookie(HttpContext
    context, String key, String value, CookieOptions options)</strong>    at
    <strong>Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler.HandleSignInAsync(ClaimsPrincipal
    user, AuthenticationProperties properties)</strong>    <strong>at
    Microsoft.AspNetCore.Authentication.AuthenticationService.SignInAsync(HttpContext
    context, String scheme, ClaimsPrincipal principal,
    AuthenticationProperties properties)</strong>    at
    Microsoft.AspNetCore.Identity.SignInManager<code>1.SignInWithClaimsAsync(TUser
    user, AuthenticationProperties authenticationProperties, IEnumerable</code>1
    additionalClaims)    at GroupMembersInfo.Pages.RegisterUser.Register()
    in
    C:\my_work\Blazor_learning\GroupMembersInfo\Pages\RegisterUser.razor:line
    52    at
    Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task
    task)    at
    Microsoft.AspNetCore.Components.Forms.EditForm.HandleSubmitAsync()<br>
    at
    Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task
    task)    at
    Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task
    taskToHandle)</p>
  </blockquote>
  
  <p>I think the exception is being thrown from the method I have highlighted. So How do I mitigate this issue. Here my code</p>
  
  <p>starup.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  using Microsoft.AspNetCore.Builder;
  using Microsoft.AspNetCore.Components;
  using Microsoft.EntityFrameworkCore;
  using Microsoft.AspNetCore.Hosting;
  using Microsoft.Extensions.Configuration;
  using Microsoft.Extensions.DependencyInjection;
  using Microsoft.Extensions.Hosting;
  using GroupMembersInfo.Data;
  using Microsoft.AspNetCore.Identity;
  using Microsoft.AspNetCore.Components.Authorization;
  using Microsoft.AspNetCore.Components.Server;
  
  namespace GroupMembersInfo
  {
      public class Startup
      {
          public Startup(IConfiguration configuration)
          {
              Configuration = configuration;
          }
  
          public IConfiguration Configuration { get; }
  
          // This method gets called by the runtime. Use this method to add services to the container.
          // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
          public void ConfigureServices(IServiceCollection services)
          {
              services.AddDbContextPool&lt;AppDbContext&gt;(
                  options =&gt; options.UseSqlServer(Configuration.GetConnectionString("GMIDbConnection")));
  
              services.AddIdentity&lt;IdentityUser, IdentityRole&gt;(options =&gt; options.SignIn.RequireConfirmedAccount = true)
                  .AddEntityFrameworkStores&lt;AppDbContext&gt;();
  
              services.AddRazorPages();
              services.AddServerSideBlazor();
              services.AddScoped&lt;AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider&lt;IdentityUser&gt;&gt;();
              services.AddSingleton&lt;WeatherForecastService&gt;();
  
              services.ConfigureApplicationCookie(options =&gt;
              {
                  options.SlidingExpiration = true;
  
                  options.Events.OnRedirectToLogin = cxt =&gt;
                  {
                      cxt.Response.StatusCode = 401;
                      return Task.CompletedTask;
                  };
  
                  options.Events.OnRedirectToAccessDenied = cxt =&gt;
                  {
                      cxt.Response.StatusCode = 403;
                      return Task.CompletedTask;
                  };
  
                  options.Events.OnRedirectToLogout = cxt =&gt; Task.CompletedTask;
              });
          }
  
          // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
          public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
          {
              if (env.IsDevelopment())
              {
                  app.UseDeveloperExceptionPage();
              }
              else
              {
                  app.UseExceptionHandler("/Error");
              }
  
              app.UseHttpsRedirection();
              app.UseStaticFiles();
  
              app.UseRouting();
  
              app.UseAuthentication();
              app.UseAuthorization();
  
              app.UseEndpoints(endpoints =&gt;
              {
                  endpoints.MapBlazorHub();
                  endpoints.MapFallbackToPage("/_Host");
              });
          }
      }
  }
  </code></pre>
  
  <p>Login Component Code</p>
  
  <pre><code>@page "/loginPage"
  
  @using Microsoft.AspNetCore.Identity;
  @using GroupMembersInfo.Data;
  
  @inject UserManager&lt;IdentityUser&gt; UserManager
  @inject SignInManager&lt;IdentityUser&gt; SignInManager
  @inject NavigationManager NavigationManager
  
  &lt;h3&gt;Log In&lt;/h3&gt;
  
  &lt;div class="row"&gt;
      &lt;div class="col-md-12"&gt;
          &lt;EditForm Model="@RegisterUserModel" OnValidSubmit="@LogIn"&gt;
              &lt;div class="form-group"&gt;
                  &lt;label&gt;Iser Id: &lt;/label&gt;
                  &lt;input @bind-value="RegisterUserModel.Email" class="form-control" /&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;label&gt;Password: &lt;/label&gt;
                  &lt;input @bind-value="RegisterUserModel.Password" class="form-control" /&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;button type="submit"&gt;Submit&lt;/button&gt;
              &lt;/div&gt;
          &lt;/EditForm&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  @code {
      public RegisterUserModel RegisterUserModel { get; set; } = new RegisterUserModel();
  
      public async Task LogIn()
      {
          var user = new IdentityUser
          {
              UserName = RegisterUserModel.Email
          };
  
          user.PasswordHash = SignInManager.UserManager.PasswordHasher.HashPassword(user, RegisterUserModel.Password);
  
          await SignInManager.SignInAsync(user, isPersistent: false);
  
          NavigationManager.NavigateTo("/");
      }
  }
  </code></pre>