ASP.Net MVC Cookie 不持久

发布于 2024-11-05 08:28:50 字数 1668 浏览 0 评论 0原文

本质上,我试图在用户登录后设置一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄情伤 2024-11-12 08:28:50

添加到response.cookies而不是request.cookies

Add to response.cookies not request.cookies

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文