Когда я запускаю код , если имя пользователя и пароль совпадают (означает, что программа достигает строки: «await HttpContext.SignInAsync(...)», и я получаю эту ошибку:
InvalidOperationException: для схемы MyCookieAuth не зарегистрирован обработчик аутентификации входа. Зарегистрированные схемы входа: Cookies. Вы забыли вызвать AddAuthentication().AddCookie("MyCookieAuth",...)?
вот функция:
Код: Выделить всё
public async Task OnPostAsync()
{
if (!ModelState.IsValid) return Page();
// verify the credential
if (string.Equals(Credential.Username, "admin") && string.Equals(Credential.Password, "p"))
{
// creating the security context
var claims = new List() {
new Claim(ClaimTypes.Name, "admin"),
new Claim(ClaimTypes.Email, "admin@domain.com")
};
var identity = new ClaimsIdentity(claims, "MycookieAuth");
// security context is within ClaimsPrinicipal
ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(identity);
// cerialises the claimsPrincipl into a stream and then encrypt that and then saves it as a cookie in the HttpContext
await HttpContext.SignInAsync("MyCookieAuth", claimsPrincipal);
return Redirect("/Index");
}
return Page();
}
Код: Выделить всё
...
builder.Services.AddAuthentication()
.AddCookie(options =>
{
options.Cookie.Name = "MycookieAuth";
});
...
это точный код тот же код, что написан в учебнике. Я ожидал, что на страницу будет отправлен файл cookie контекста безопасности, но он не работает. Служба аутентификации не запущена или, возможно, требуется дополнительная настройка?
Подробнее здесь: https://stackoverflow.com/questions/791 ... -no-sign-i
Мобильная версия