Wednesday, 30 November 2011

cookies concept in asp.net

o write a cookie by creating an instance of the HttpCookie object

  1. Create an object of type HttpCookie and assign it a name.
  2. Assign values to cookie's subkeys and set any cookie properties.
  3. Add the cookie to the Cookies collection.
    The following code example shows an instance of the HttpCookie object named myCookie, which represents a cookie named UserSettings.
    HttpCookie myCookie = new HttpCookie("UserSettings");
    myCookie["Font"] = "Arial";
    myCookie["Color"] = "Blue";
    myCookie.Expires = DateTime.Now.AddDays(1d);
    Response.Cookies.Add(myCookie);
    Read a string from the Cookies collection using the cookie's name as the key.
     
    if (Request.Cookies["UserSettings"] != null)
    {
        string userSettings;
        if (Request.Cookies["UserSettings"]["Font"] != null)
        { userSettings = Request.Cookies["UserSettings"]["Font"]; }
    }
     

No comments:

Post a Comment