c# - 登录时在身份 cookie 中存储多个用户值
问题描述
我在用户登录时有一段代码,如下所示:
ClaimsIdentity identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie);
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, model.Username));
List<Claim> claims = new List<Claim>();
var roles = user.UserRoles.ToList();
foreach (var item in roles)
{
claims.Add(new Claim(ClaimTypes.Role, item.Roles.RoleName));
}
identity.AddClaims(claims);
identity.AddClaim(new Claim(ClaimTypes.Name, user.UserId.ToString()));
AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = true, ExpiresUtc = DateTimeOffset.UtcNow.AddHours(8) }, identity);
因此,一旦执行了这段代码,我就可以访问其他控制器中的用户角色和用户 ID,如下所示:
User.Identity.Name // => getting user id of current logged user
并且:
User.IsInRole("Regular"); // => getting user role in system
现在我需要更多的变量,这些变量会在用户登录时存储在 cookie 中,例如:
// User theme => // I have multiple CSS files relating to different styles of page and I'd need to store this information in Identity as well.
// User preferences => multiple preferences as well
所以在一切结束时,我可以访问这样的信息:
User.Identity... // user theme
User.Identity... // user preferences
我怎样才能做到这一点 ?
有人可以帮我吗 ?