【发布时间】:2012-07-01 06:01:37
【问题描述】:
我正在尝试使用以下代码(在 AccountController.cs 中)将 FormsAuthenticationTicket 保存到 cookie:
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket
(1, user.UserEmail, DateTime.Now,
DateTime.Now.AddMinutes(FormsAuthentication.Timeout.TotalMinutes),
false, null);
string encTicket = FormsAuthentication.Encrypt(ticket);
HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName,
ticket.ToString());
HttpContext.Response.Cookies.Add(faCookie);
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
当我单步调试调试器时,一切似乎都很好。直到我到达Application_AuthenticateRequest,在那里我尝试检索 cookie:
HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
//do stuff here
}
当我查看 Cookies 集合时,那里什么都没有。我可以在 AccountController 代码中添加另一个普通 cookie,它显示得很好。无论我是否包含 UserData,问题仍然存在,所以我认为这不是大小问题。
感谢您提供的任何见解。
【问题讨论】:
标签: asp.net-mvc-3 cookies formsauthenticationticket