How to create a persistent cookie
I want to create a persistent cookie, how to create it.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Praveen VR65 reputationPosted Jul 15, 2012, 9:31 PM EDT
You can do this with the following code:
HttpCookie _cookie1 = new HttpCookie("LocalComputer");
_cookie1.Values.Add("username", username);
_cookie1.Expires = DateTime.Now.AddMonths(1);
Response.Cookies.Add(_cookie1);
And you can call this cookie with the following code:
if (Request.Cookies["LocalComputer"] != null)
{
HttpCookie _cookie = Request.Cookies["LocalComputer"];
string username=_cookie["username"].toString();
}
Satyapriya NayakRank #47 · 31.5K reputationPosted Jul 15, 2012, 10:18 PM EDT
Please refer the below links
http://www.codeproject.com/Articles/31914/Beginner-s-Guide-To-ASP-NET-Cookies
http://ashrafur.wordpress.com/2008/01/11/persistent-and-non-persistent-cookies-in-aspnet/
http://www.java2s.com/Tutorial/ASP.NET/0240__Cookie/Createapersistentcookie.htm
Thanks
Sachin KaliaRank #102 · 20.8K reputationPosted Jul 15, 2012, 10:48 PM EDT
The easist way to create persisted cookie is under here:
//create a cookie HttpCookie myCookie = new HttpCookie("myCookie"); //Add key-values in the cookie myCookie.Values.Add("userid", objUser.id.ToString()); //set cookie expiry date-time. Made it to last for next 12 hours. myCookie.Expires = DateTime.Now.AddHours(12); //Most important, write the cookie to client. Response.Cookies.Add(myCookie);http://stackoverflow.com/questions/3140341/how-to-create-persistent-cookies-in-asp-net
Chees DotNet :)
vijay172 reputationPosted Jul 27, 2012, 5:57 AM EDT
Refer the below link to create persistent cookies.
http://www.dotnetcode.in/2011/08/store-user-name-in-cookies-using-aspnet.html