views:

403

answers:

1

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);
}
+3  A: 

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.

Paul Alexander
+1 This saved my bacon - thank you Paul!
n8wrl