Domanda

I want to store user latest visited pages in a cookie. It has 2 parts, PageTitle and URL. I use the code below but it just save a value in first page load and don't change it in other page loads.

 if (Request.Cookies["latestvisit"] == null)
    {
        HttpCookie myCookie = new HttpCookie("latestvisit");
        myCookie.Expires = DateTime.Now.AddYears(1);
        myCookie.Values[title] = System.Web.HttpUtility.UrlEncode(URL);
        Response.Cookies.Add(myCookie);
    }
    else
    {
        System.Collections.Specialized.NameValueCollection cookieCollection = Request.Cookies["latestvisit"].Values;
        string[] CookieTitles = cookieCollection.AllKeys;

        //mj-y: If the url is reapeated, move it to end(means make it newer by removing it and adding it again)
        string cookieURL = "";
        foreach (string cookTit in CookieTitles)
        {
            cookieURL = System.Web.HttpUtility.UrlDecode(Request.Cookies["latestvisit"].Values[cookTit]);
            if (cookieURL == URL)
            {
                cookieCollection.Remove(cookTit);
                cookieCollection.Set(title, URL);
                return;
            }
        }
        //mj-y: If it was not repeated ...
        if (cookieCollection.Count >15) // store just 15 item         
            cookieCollection.Remove(CookieTitles[0]);          
        cookieCollection.Set(title, URL);
    }

and of course I want to code the url and decode it, so user cant determine the content of the cookie, how can I do it?

È stato utile?

Soluzione

Try this:

if (Request.Cookies["latestVisit"] == null)
{
    HttpCookie myCookie = new HttpCookie("latestVisit");
    myCookie.Expires = DateTime.Now.AddYears(1);
    myCookie.Values[title] = System.Web.HttpUtility.UrlEncode(URL);
    Response.Cookies.Add(myCookie);
}
else
{
    var myCookie = Request.Cookies["latestVisit"];
    var cookieCollection = myCookie.Values;
    string[] CookieTitles = cookieCollection.AllKeys;

    //mj-y: If the url is reapeated, move it to end(means make it newer by removing it and adding it again)
    string cookieURL = "";
    foreach (string cookTit in CookieTitles)
    {
        cookieURL = System.Web.HttpUtility.UrlDecode(Request.Cookies["latestVisit"].Values[cookTit]);
        if (cookieURL == URL)
        {
            cookieCollection.Remove(cookTit);
            cookieCollection.Set(title, System.Web.HttpUtility.UrlEncode(URL));
            Response.SetCookie(myCookie);
            return;
        }
    }
    //mj-y: If it was not repeated ...
    cookieCollection.Set(title, System.Web.HttpUtility.UrlEncode(URL));
    if (cookieCollection.Count > 15) // store just 15 item         
        cookieCollection.Remove(CookieTitles[0]);
    Response.SetCookie(myCookie);
}

As a safe practice, I'd also recommend you to encode the title variable before adding it to the values collection, for example:

myCookie.Values[System.Web.HttpUtility.UrlEncode(title)] 
    = System.Web.HttpUtility.UrlEncode(URL);

and

cookieCollection.Set(System.Web.HttpUtility.UrlEncode(title), 
    System.Web.HttpUtility.UrlEncode(URL));
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top