Мое дерево страниц:

Код: Выделить всё
Program.csКод: Выделить всё
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(x =>
{
x.LoginPath = "/login";
});
builder.Services.AddAuthorization();
builder.Services.AddCascadingAuthenticationState();
///////////////////////////
app.UseAuthentication();
app.UseAuthorization();
Код: Выделить всё
Login.razorКод: Выделить всё
@using Microsoft.AspNetCore.Authentication
@using Microsoft.AspNetCore.Authentication.Cookies
@using System.Security.Claims
@* some razor component *@
@code{
[CascadingParameter]
public HttpContext httpcontext { get; set; } = default!;
public async Task ClickLogin()
{
_loginbtnLoading = true;
if (!string.IsNullOrEmpty(_ntid) && !string.IsNullOrEmpty(_password))
{
await Task.Delay(2000);
//I'm using LDAP for checking user signin
var loginOK = ILoginServices.CheckLoginStatus(_ntid, _password);
if (loginOK.success)
{
var claims = new List();
claims.Add(new Claim(ClaimTypes.Name, _ntid)); // add more claims
var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var principal = new ClaimsPrincipal(claimsIdentity);
// Sign in the user
await httpcontext.SignInAsync(principal);
NavManager.NavigateTo("/home");
}
else
{
Snackbar.Add(loginOK.errorMessage, Severity.Error);
}
}
else
{
Snackbar.Add("Please key in all the required info", Severity.Error);
}
_loginbtnLoading = false;
}
}
Код: Выделить всё
System.InvalidOperationException: Headers are read-only, response has already started.Подробнее здесь: https://stackoverflow.com/questions/781 ... t-identity