locked
How to create a cookie RRS feed

  • Question

  • User-654052387 posted

    Way 1 (by using HttpCookies class)

    HttpCookie StudentCookies = new HttpCookie("StudentCookies");
    StudentCookies.Value = TextBox1.Text;
    StudentCookies.Expires =
    DateTime.Now.AddHours(1);
    Response.Cookies.Add(StudentCookies);

    multiple values in same cookie

    HttpCookie userInfo = new HttpCookie("userInfo");

    userInfo["UserName"] = "Annathurai";

    userInfo["UserColor"] = "Black";

    userInfo.Expires.Add(new TimeSpan(0, 1, 0));

    Response.Cookies.Add(userInfo);

    Way 2 (by using Response directly)

    Response.Cookies["StudentCookies"].Value = TextBox1.Text;
    Response.Cookies["StudentCookies"].Expires = DateTime.Now.AddDays(1);

    multiple values in same cookie

    Response.Cookies["StudentCookies"]["RollNumber"] = TextBox1.Text;
    Response.Cookies["StudentCookies"]["FirstName"] = "Abhimanyu";
    Response.Cookies["StudentCookies"]["LastName"] = "Vatsa";
    Response.Cookies["StudentCookies"]["TotalMarks"] = "499";
    Response.Cookies["StudentCookies"].Expires = DateTime.Now.AddDays(1);

    Saturday, December 7, 2013 9:29 PM

All replies