实现用.NET Identity记住我

时间:2017-01-29 13:55:49

标签: c# asp.net asp.net-mvc asp.net-identity remember-me

我写过如下代码:

   if (user.PasswordHash == Helpers.PasswordHelper.CreatePasswordHash(model.Password, user.PasswordSalt))
                        {

                            ClaimsIdentity identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie);
                            identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, model.Email));
                            List<Claim> claims = new List<Claim>();
                            var roles = user.UserRoles.Where(x => x.UserId == user.UserId).ToList();

                            foreach (var item in roles)
                            {
                                claims.Add(new Claim(ClaimTypes.Role, item.Roles.RoleName));
                            }
                            identity.AddClaims(claims);
                            identity.AddClaim(new Claim(ClaimTypes.Name, model.Email));

                            AuthenticationManager.SignIn(identity);

                            return RedirectToAction("Index", "Dashboard");
                        }

在我的模型中,我有记住我的属性,我想在用户登录时加入,我该怎么做?

P.S。价值是bool,我只需要以某种方式告诉浏览器用户从登录菜单中选择了什么......

1 个答案:

答案 0 :(得分:1)

试试这个:

using Microsoft.Owin.Security;

...

AuthenticationManager.SignIn(new AuthenticationProperties(){IsPersistent = model.RememberMe}, identity);