Mike Perrenoud
Mike Perrenoud

Reputation: 67898

Duplicate cookies?

I have an application that leverages a cookie to support a quasi-wizard (i.e. it's a set of pages that are navigated to by each other, and they must occur in a specific order, for registration).

When the Logon.aspx page is loaded - the default page - the browsers cookies look right.

Here we have only one cookie.

There's one cookie and it has the right value. This ensures that the next page, which is an enrollment agreement, knows that it was loaded from the Logon.aspx page. However, when I get to that page the browsers cookies look much different:

Now we have two of the same cookie.

Now we have two of the same cookie.

This doesn't appear to be causing any real issues - but I can't be sure it won't. So, let me show you the code I'm using to set the cookie (because maybe there's something wrong with it):

if (!this.IsPostBack)
{
    Utility.HandleReferrer(Request, Response, "Logon.aspx");
    Response.Cookies["lastpage"].Value = "Enroll.aspx";
}

and the HandleReferrer method looks like this:

static public void HandleReferrer(HttpRequest request, HttpResponse response, string expectedReferrer)
{
    var cookie = request.Cookies["lastpage"];
    if (cookie != null && cookie.Value.ToLower().Contains(expectedReferrer.ToLower()))
    {
        return;
    }

    response.Redirect("Logon.aspx");
}

So, why in the world does it duplicate this cookie? It doesn't ever seem to create more than two.

Upvotes: 3

Views: 3025

Answers (2)

Dedrael
Dedrael

Reputation: 11

I tried another approach, by creating a method that will return the latest cookie occurrence, this way I'll always get the right data.

This method expect the collection of cookies from Request, and the name of the searched cookie, and returns the latest ticket (the information that is normally encrypted)

private static FormsAuthenticationTicket GetLatestCookie(HttpCookieCollection cookies, string cookieName) {
    var cookieOccurrences = new List<FormsAuthenticationTicket>();

    for (int index = 0; index < cookies.Count; index++) {
        if (cookies.GetKey(index) == cookieName) {
            FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookies[index].Value);
            cookieOccurrences.Add(ticket);
        }
    }

    DateTime oldestTime = DateTime.MinValue;
    FormsAuthenticationTicket oldestTicket = null;
    foreach (var formsAuthenticationTicket in cookieOccurrences) {
        if (formsAuthenticationTicket.Expiration > oldestTime) {
            oldestTime = formsAuthenticationTicket.Expiration;
            oldestTicket = formsAuthenticationTicket;
        }
    }

    return oldestTicket;
}

Upvotes: 0

ChrisLively
ChrisLively

Reputation: 88064

I suggest you do one of the following.

First, get the latest glimpse and try again.

If it is still showing 2 cookies with that name then get firebug and/or fiddler and look at it that way. If I had to take a guess I'd say there's either something wrong in glimpse or something wrong in how you are interpreting the results. Perhaps glimpse is showing what cookies existed before and then after the request was processed?

A third option is to simply emit the cookies collection from your own .net code. Something like:

foreach(HttpCookie cookie in request.Cookies) {
  Response.Write(String.Format("{0} = {1}", cookie.Name, cookie.Value));
}

and see what happens.

Upvotes: 2

Related Questions