user34537
user34537

Reputation:

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

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);
}

Upvotes: 3

Views: 6161

Answers (1)

Paul Alexander
Paul Alexander

Reputation: 32367

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.

Upvotes: 4

Related Questions