为什么进行重定向时未设置cookie? [英] Why are the cookies not being set when doing a Redirect?
本文介绍了为什么进行重定向时未设置cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
或者也许我做错了,为什么在进行重定向时未设置cookie?
Or maybe i am doing it wrong, why are the cookies not being set when doing a Redirect?
static void doLogin()
{
var req = HttpContext.Current.Request;
...
user_cookie.set(userId, loginId);
...
HttpContext.Current.Response.Redirect(req["returnLocation"]);
}
static public void set(long userId, long loginId)
{
var cookies = HttpContext.Current.Request.Cookies;
var u = new HttpCookie("userId", userId.ToString());
u.HttpOnly = true;
var l = new HttpCookie("loginId", loginId.ToString());
l.HttpOnly = true;
cookies.Add(u);
cookies.Add(l);
}
推荐答案
您正在将cookie添加到请求.Cookies集合,您希望将其添加到响应.改为收集Cookie.
You're adding cookies to the Request.Cookies collection, you'll want to add them to the Response.Cookies collection instead.
还要注意,Response.Redirect将中止我所见过的当前线程,这有时会导致问题.Response.Redirect(url,false)将重定向而不会中止.
Also note that Response.Redirect will abort the current thread which I've seen cause problems on occasion. Response.Redirect( url, false ) will redirect without aborting.
这篇关于为什么进行重定向时未设置cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文