修改 HttpHandler 中 Cookie 的值
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试添加
.Value
Try adding
.Value
根据 MSDN 站点,您已经编写了一个具有相同内容的新 cookie名称,而不仅仅是修改它:
我同意第一篇关于添加 .Value 属性的文章,然后也可能添加 .Expires 并看看会发生什么。
According to the MSDN site, you have write a new cookie with the same name, not just modify it:
I'd agree with the first post about adding the .Value property and then maybe add the .Expires as well and see what happens.