【发布时间】:2015-02-20 15:27:42
【问题描述】:
我想知道当用户点击记住我复选框时,如何通过 cookie 设置 @User.Identity.Name。
Cookie 代码
if (_model.RememberMe)
{
HttpCookie cookie = new HttpCookie("login");
cookie.Values.Add("username", _model.Username);
cookie.Expires = DateTime.Now.AddDays(30);
Response.Cookies.Add(cookie);
}
首次登录代码
if (Membership.ValidateUser(Username,Password))
{
RememberMe();
FormsAuthentication.RedirectFromLoginPage(Username, false);
}
在第一次登录时设置了 Identity.name,但是当我关闭浏览器并返回该站点时。它无需用户输入凭据即可正确登录,但Identity.name 为空。
if (Request.Cookies["login"] != null)
{
// We know the automatic log in has worked as it comes into here...
}
用户通过登录页面后,我需要做什么才能设置iPrincipal 对象?
谢谢
【问题讨论】:
-
不能直接设置
User.Identity.Name,需要创建FormsAuthenticationTicketmsdn.microsoft.com/en-us/library/… -
哇,就是这么简单。谢谢你一整天都在做我。让它成为一个答案,如果你喜欢它会标记它
标签: c# asp.net-mvc cookies forms-authentication