Ядро asp.net не перенаправляет на правильные страницы входа
Настроен таким образом, он не работает.
services
.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.ExpireTimeSpan = TimeSpan.FromMinutes(5);
options.LoginPath = $"/logon";
options.LogoutPath = $"/logoff";
options.AccessDeniedPath = $"/accessdenied";
options.SlidingExpiration = true;
})
настроил так, чтобы он работал:
services.ConfigureApplicationCookie(options =>
{
options.Cookie.Name = "Caldr.Auth";
options.LoginPath = $"/logon";
options.LogoutPath = $"/logoff";
options.AccessDeniedPath = $"/accessdenied";
});
services
.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
Я ожидаю, что оба будут иметь одинаковое поведение. Очевидно, нет. Баг или у меня не получилось как это настроить?:-)
Какие-нибудь мысли.
1 ответ
Решение
На момент публикации моего вопроса у меня тоже была настроена / добавлена структура идентификации. Таким образом, это могло быть сочетание нескольких факторов, которые могут не работать должным образом.
Рабочий раствор:
CONFIG:
var authenticationBuilder = services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = $"/logon";
options.LogoutPath = $"/logoff";
options.AccessDeniedPath = $"/accessdenied";
});
ConfigureSocialLogins(authenticationBuilder);
Фактический вход в систему (т.е. запись файлов cookie осуществляется через)
private async Task SignInUser(AppUser appUser)
{
var claims = new List<Claim>
{
new Claim(ClaimTypes.NameIdentifier, appUser.Email),
new Claim(ClaimTypes.Name, appUser.Displayname ?? appUser.Email),
new Claim(ClaimTypes.Email, appUser.Email),
};
var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var claimsPrincipal = new ClaimsPrincipal(claimsIdentity);
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal, new AuthenticationProperties());
}
Обратите внимание на все случаи использования CookieAuthenticationDefaults.AuthenticationScheme.