gpt4 book ai didi

asp.net - ASP.Net MVC Cookies不持久

转载 作者:行者123 更新时间:2023-12-04 14:05:37 24 4
gpt4 key购买 nike

本质上,我试图在用户登录后设置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);
}

最佳答案

添加到response.cookies不是request.cookies

关于asp.net - ASP.Net MVC Cookies不持久,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5876573/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com