I know this question has been asked many time, but I am yet to find an answer to this issue
Here is my Blazor login page
@page "/login"@rendermode InteractiveServer@layout Components.Layout.MinimalLayout@using Microsoft.AspNetCore.Authentication.Cookies@using Microsoft.AspNetCore.Identity@using System.ComponentModel.DataAnnotations@using MyBlazorApp.Middleware@using System.Security.Claims@using Microsoft.AspNetCore.Authentication;@inject SignInManager<IdentityUser> SignInManager@inject NavigationManager Navigation@inject UserManager<IdentityUser> UserMgr@inject IHttpContextAccessor _httpContextAccessor<h3>Login</h3>@if (!string.IsNullOrEmpty(errorMessage)){<div class="alert alert-danger">@errorMessage</div>}<EditForm Model="loginModel" OnValidSubmit="HandleLogin" FormName="LoginForm"><DataAnnotationsValidator /><ValidationSummary /><div class="mb-3"><label>Email</label><InputText class="form-control" @bind-Value="loginModel.Email" /></div><div class="mb-3"><label>Password</label><InputText class="form-control" @bind-Value="loginModel.Password" type="password" /></div><button class="btn btn-primary" type="submit">Login</button><p><a href="/register">Don't have an account? Register here.</a></p></EditForm>@code { private LoginModel loginModel = new(); private string? errorMessage; private async Task HandleLogin() { try { errorMessage = null; var usr = await UserMgr.FindByEmailAsync(loginModel.Email); if (usr == null) { errorMessage = "User not found"; return; } if (!await SignInManager.CanSignInAsync(usr)) { errorMessage = "Your account is blocked"; return; } // Only call PasswordSignInAsync ONCE var result = await SignInManager.PasswordSignInAsync(loginModel.Email, loginModel.Password, false, lockoutOnFailure: true); if (result.Succeeded) { Navigation.NavigateTo("/", forceLoad: true); } else { errorMessage = "Invalid login attempt."; } } catch (Exception ex) { errorMessage = ex.Message; } } public class LoginModel { [Required(ErrorMessage = "Email is required.")] [EmailAddress(ErrorMessage = "Invalid email address.")] public string Email { get; set; } = string.Empty; [Required(ErrorMessage = "Password is required.")] public string Password { get; set; } = string.Empty; }}Here is my program.cs
using Microsoft.AspNetCore.Authentication.Cookies;using Microsoft.AspNetCore.Identity;using Microsoft.EntityFrameworkCore;using MyBlazorApp.Components;using MyBlazorApp.Data;using MyBlazorApp.Middleware;var builder = WebApplication.CreateBuilder(args);// Add services to the container.builder.Services.AddRazorComponents() .AddInteractiveServerComponents();builder.Services.AddDbContext<MyDbContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));builder.Services.AddAuthentication(options =>{ options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;}) .AddCookie();builder.Services.AddAuthorization();builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = false) .AddEntityFrameworkStores<MyDbContext>().AddRoles<IdentityRole>().AddEntityFrameworkStores<MyDbContext>().AddSignInManager().AddDefaultTokenProviders();builder.Services.AddCascadingAuthenticationState();builder.Services.AddAuthorizationCore();var app = builder.Build();// Configure the HTTP request pipeline.if (!app.Environment.IsDevelopment()){ app.UseExceptionHandler("/Error", createScopeForErrors: true); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts();}app.UseHttpsRedirection();app.UseAntiforgery();app.MapStaticAssets();app.UseAuthentication();app.UseAuthorization();app.MapRazorComponents<App>() .AddInteractiveServerRenderMode();app.Run();However, no matter what I do, I can't login, I ALWAYS get this error
Headers are read-only, response has already started
Look like a very mysterious error no one can resolve. Anyone?
Thank you very much in advance