views:

60

answers:

1

When a user logs in to my site, I create a cookie with some info in it. However, whenever they change page from logging in, the cookie loses it's value. Cookie is still there but it's empty.

I've checked my code and the cookie doesn't get rewritten by anything I've done. Does anyone have any idea to why the cookie becomes empty when the page is changed?

Here's the method for creating the cookie.

public static void CreateUserCookie(long userId, string username, bool rememberMe) {

        HttpCookie cookie = new HttpCookie("CookieName");
        cookie.Value = string.Format("{0}+{1}+{2}", userId, username, SecurityUtils.CreateHashedCookieValue(userId, username));

        if (rememberMe) {
            cookie.Expires = DateTime.Now.AddMonths(1);
        } else {
            cookie.Expires = DateTime.MinValue;
        }

        HttpContext.Current.Response.Cookies.Add(cookie);
    }
+1  A: 

When you call this method, do you pass in true for the "rememberMe" parameter? If not, the cookie will instantly expire.

You haven't shown your calling code, so this is potentially what you've done.

Sohnee
So the cookie will instantly expire with DateTime.MinValue? What should I enter instead to make it expire at the end of the browser session?
Jova
If you pass in "true" it will expire in one month, or you can change that so it will be a different time period - 20 minutes, a day etc.
Sohnee