【发布时间】:2014-04-05 10:43:11
【问题描述】:
我正在使用 C# 和 MVC5 使用以下代码创建自己的 cookie:
// Prepare the ticket
HttpContext.Response.Cookies.Clear();
FormsAuthenticationTicket ticket =
new FormsAuthenticationTicket(1,
"MYNAME",
DateTime.Now,
DateTime.Now.AddDays(10), // <<- Expires 10 days
true,
null);
// Encrpt the ticket
string encryptedCookie = FormsAuthentication.Encrypt(ticket);
// Create new cookie
HttpCookie cookie = new HttpCookie("MYNAME", encryptedCookie);
cookie.Path = FormsAuthentication.FormsCookiePath;
// Send the Cookie back to the browser
HttpContext.Response.Cookies.Add(cookie);
在 Web.Config 中,我将名称设置为
<authentication mode="Forms">
<forms name="MYNAME" loginUrl="~/Account/Login"></forms>
</authentication>
但是当我查看 Firebug 时,Cookie 显示为“MYNAME”,但“expires”设置为 Session 。
事实上,当我关闭浏览器时,cookie 消失了,当我回到网站时,我总是需要重新登录。所有其他浏览器也是如此。
我做错了什么??
【问题讨论】:
标签: c# asp.net-mvc cookies web-config remember-me