Вот моя страница входа в Blazor
Код: Выделить всё
@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 SignInManager
@inject NavigationManager Navigation
@inject UserManager UserMgr
@inject IHttpContextAccessor _httpContextAccessor
Login
@if (!string.IsNullOrEmpty(errorMessage))
{
@errorMessage
}
Email
Password
Login
[url=/register]Don't have an account? Register here.[/url]
@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;
}
}
Код: Выделить всё
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(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie();
builder.Services.AddAuthorization();
builder.Services.AddDefaultIdentity(options => options.SignIn.RequireConfirmedAccount = false)
.AddEntityFrameworkStores()
.AddRoles()
.AddEntityFrameworkStores()
.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()
.AddInteractiveServerRenderMode();
app.Run();
Заголовки доступны только для чтения, ответ уже начался.
Похоже, это очень загадочная ошибка, которую никто не может устранить. Кто-нибудь?
Заранее большое спасибо
Подробнее здесь: https://stackoverflow.com/questions/797 ... lling-sign
Мобильная версия