В program.cs:
Код: Выделить всё
builder.Services
.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(
options =>
{
options.Cookie.Name = "cookie name";
options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromDays(1);
options.SlidingExpiration = false;
});
Код: Выделить всё
.UseAuthentication()
.UseAuthorization()
Код: Выделить всё
var claims = AssertPermissionClaims(user);
var identity = new ClaimsIdentity(claims,
CookieAuthenticationDefaults.AuthenticationScheme);
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new
ClaimsPrincipal(identity));
//AssertPermissionClaims method
private static Claim[]? AssertPermissionClaims(AppUser user)
{
var claims = new[] {
new Claim(ClaimTypes.Name, user?.UserName ?? ""),
new Claim(ClaimTypes.Role, user?.RoleName ?? ""),
new Claim(ClaimTypes.UserData, user==null ||
user.UserId==null?"":user.UserId.ToString())
};
return claims;
}
Код: Выделить всё
User?.Identity?.IsAuthenticated
Код: Выделить всё
HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.UserData)?.Value
Ищу ответ. Спасибо.
Обновление: проблема возникает только с рабочим сервером, сервером разработки, он работает нормально. Пробовал также использовать специальную реализацию SessionStore. То же самое происходит.
Подробнее здесь: https://stackoverflow.com/questions/786 ... ened-in-de
Мобильная версия