Why are the cookies not being set when doing a Redirect?

一笑奈何 提交于 2020-01-03 19:40:54

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!