【发布时间】:2015-04-30 17:36:27
【问题描述】:
我正在尝试向我的网页添加一个选项,以便在用户登录时拥有“记住我”选项。为了实现这一点,我试图将用户数据(名称+密码)存储在 cookie 中,但目前无法正常工作。 我存储 cookie 的代码是:
int timeout = 525600;
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(model.UserName, true, timeout);
string encrypted = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted);
cookie.Expires = System.DateTime.Now.AddMinutes(timeout);
System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
FormsAuthentication.SetAuthCookie(model.UserName, true);
Request.Cookies.Add(cookie);
在我的登录控制器上,我的代码如下所示:
HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies.Get(FormsAuthentication.FormsCookieName);
if (cookie != null)
{
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);
var userName = ticket.UserData;
}
问题在于 userData cookie 始终为空。我错过了什么?
【问题讨论】:
-
客户端是否接受cookies?
-
是的,它接受 cookie
标签: c# asp.net-mvc-4 cookies