ASP.Net MVC Cookie 不持久
本质上,我试图在用户登录后设置一个 cookie,以便在下次登录时保留他们的用户名。这是我设置 cookie 的代码。当我在设置 cookie 后立即在 Firefox 中查看站点 cookie 时,它会显示 sessionID cookie,而不是我刚刚设置的 cookie。当我检查 Fiddler 中的标头时,我没有看到它设置 cookie,只有我的 sessionID cookie。
HttpCookie hc = new HttpCookie("username", model.UserName);
hc.Expires = DateTime.Now.AddYears(1);
System.Web.HttpContext.Current.Request.Cookies.Add(hc);
这是我检查 cookie 是否存在的地方。
if (System.Web.HttpContext.Current.Request.Cookies["username"] != null)
这是相关方法的完整上下文
public ActionResult LogOn()
{
if (System.Web.HttpContext.Current.Request.Cookies["username"] != null)
return View(new LogOnModel { UserName = System.Web.HttpContext.Current.Request.Cookies["username"].Value });
else
return View();
}
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (MembershipService.ValidateUser(model.UserName, model.Password))
{
HttpCookie hc = new HttpCookie("username", model.UserName);
hc.Expires = DateTime.Now.AddYears(1);
System.Web.HttpContext.Current.Request.Cookies.Add(hc);
FormsService.SignIn(model.UserName, model.RememberMe);
if (!String.IsNullOrEmpty(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
}
return View(model);
}
Essentially I'm trying to set a cookie after a user logs in to persist their username for the next time they log in. Here's my code to set the cookie. When I look at the site cookies in Firefox as soon as the cookie is set, it shows the sessionID cookie, but not the one I just set. When I check the headers in Fiddler, I don't see it setting the cookie, only my sessionID cookie.
HttpCookie hc = new HttpCookie("username", model.UserName);
hc.Expires = DateTime.Now.AddYears(1);
System.Web.HttpContext.Current.Request.Cookies.Add(hc);
Here is where I check to see if the cookie exists.
if (System.Web.HttpContext.Current.Request.Cookies["username"] != null)
Here's the full context of the methods in question
public ActionResult LogOn()
{
if (System.Web.HttpContext.Current.Request.Cookies["username"] != null)
return View(new LogOnModel { UserName = System.Web.HttpContext.Current.Request.Cookies["username"].Value });
else
return View();
}
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (MembershipService.ValidateUser(model.UserName, model.Password))
{
HttpCookie hc = new HttpCookie("username", model.UserName);
hc.Expires = DateTime.Now.AddYears(1);
System.Web.HttpContext.Current.Request.Cookies.Add(hc);
FormsService.SignIn(model.UserName, model.RememberMe);
if (!String.IsNullOrEmpty(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
}
return View(model);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加到response.cookies而不是request.cookies
Add to response.cookies not request.cookies