修改 HttpHandler 中 Cookie 的值

发布于 2024-11-14 22:19:56 字数 747 浏览 2 评论 0原文

我有一个 cookie,用来保存用户的 userid,但我很难用新值替换它。根据 MSDN,我应该能够简单地覆盖该值,但它不起作用。我正在处理程序中执行登录逻辑,如果成功,则将用户传递到新网页。

public void ProcessRequest(HttpContext context)
{
    User user = User.FindByUsernameAndPassword(
        context.Request.Form["username"],
        context.Request.Form["password"]);

    context.Response.Cookies["user_id"].Value = user.ID.ToString();

    context.Response.Redirect("/profile", true);
}

我第一次登录时效果很好,但如果我尝试通过使用新用户 ID 访问处理程序来覆盖当前的 cookie,它不会更改 cookie 值,并且我会继续以当时的用户身份登录我击中了。

其他页面使用 cookie 登录,但由于用户 ID 没有更改,因此不会更改登录的用户。

public User User { get; set; }

public override void Page_Load()
{
    this.User = User.Find(int.Parse(Request.Cookies["user_id"].Value));
}

I've got a cookie that I'm using to persist a user's userid but I'm having a hard time replacing it with a new value. According to MSDN, I should be able to simply overwrite the value, but it hasn't been working. I'm doing the login logic in a handler and passing the user on to a new webpage if they succeed.

public void ProcessRequest(HttpContext context)
{
    User user = User.FindByUsernameAndPassword(
        context.Request.Form["username"],
        context.Request.Form["password"]);

    context.Response.Cookies["user_id"].Value = user.ID.ToString();

    context.Response.Redirect("/profile", true);
}

The first time I log in it works well, but if I try to overwrite my current cookie by hitting the handler with a new user id, it doesn't change the cookie value and I continue to be logged in as the user I was when I hit it.

Other pages use the cookie to log in, but because the user id isn't changing it doesn't change the logged in user.

public User User { get; set; }

public override void Page_Load()
{
    this.User = User.Find(int.Parse(Request.Cookies["user_id"].Value));
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

绮筵 2024-11-21 22:19:56

尝试添加 .Value

context.Response.Cookies["user_id"].Value = user.ID.ToString();

Try adding .Value

context.Response.Cookies["user_id"].Value = user.ID.ToString();
又怨 2024-11-21 22:19:56

根据 MSDN 站点,您已经编写了一个具有相同内容的新 cookie名称,而不仅仅是修改它:

修改和删除 Cookie

您不能直接修改 cookie。
相反,更改 cookie 包括
使用新值创建新 cookie
然后将cookie发送到
浏览器覆盖旧版本
在客户端上。下面的代码
示例显示了如何更改
存储计数的 cookie 的值
用户访问网站的次数:

int counter;
if (Request.Cookies["counter"] == null)
    counter = 0;
else
{
    counter = int.Parse(Request.Cookies["counter"].Value);
}
counter++;

Response.Cookies["counter"].Value = counter.ToString();
Response.Cookies["counter"].Expires = DateTime.Now.AddDays(1);

我同意第一篇关于添加 .Value 属性的文章,然后也可能添加 .Expires 并看看会发生什么。

According to the MSDN site, you have write a new cookie with the same name, not just modify it:

Modifying and Deleting Cookies

You cannot directly modify a cookie.
Instead, changing a cookie consists of
creating a new cookie with new values
and then sending the cookie to the
browser to overwrite the old version
on the client. The following code
example shows how you can change the
value of a cookie that stores a count
of the user's visits to the site:

int counter;
if (Request.Cookies["counter"] == null)
    counter = 0;
else
{
    counter = int.Parse(Request.Cookies["counter"].Value);
}
counter++;

Response.Cookies["counter"].Value = counter.ToString();
Response.Cookies["counter"].Expires = DateTime.Now.AddDays(1);

I'd agree with the first post about adding the .Value property and then maybe add the .Expires as well and see what happens.

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