using System.Collections.Generic; using System.Linq; using System.Security.Claims; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Http; namespace Infrastructure.Extensions { public static class HttpContextExtensions { public static void SignIn(this HttpContext httpContext, string userName, IEnumerable roles, bool rememberMe) { var claims = new List { new Claim("Name", userName) }; claims.AddRange(roles.Select(o => new Claim("Role", o)).ToList()); var claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme, "Name", "Role")); httpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal, new AuthenticationProperties { IsPersistent = rememberMe }); } } }