问题
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);
}
回答1:
You're adding cookies to the Request.Cookies collection, you'll want to add them to the Response.Cookies collection instead.
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.
来源:https://stackoverflow.com/questions/1036889/why-are-the-cookies-not-being-set-when-doing-a-redirect