Home > DeveloperSection > Forums > How to store string in a cookie and retrieve it
Takeshi Okada
Takeshi Okada

Total Post:89

Posted on    August-25-2014 2:09 AM

 ASP.Net ASP.Net 

 1 Reply(s)
 815  View(s)
Rate this:

I want to store the username in the cookie and retrieve it the next time when the user opens the website. Is it possible to create a cookie which doesnt expires when the browser is closed. I am using asp.net c# to create the website. And how can I stop the browser from offering to save username and password

Sumit Kesarwani

Total Post:378

Posted on    August-25-2014 2:32 AM

Hi Takeshi, 

try this:

HttpCookie myCookie = new HttpCookie("MyTestCookie");

DateTime now = DateTime.Now;

// Set the cookie value.

myCookie.Value = now.ToString();

// Set the cookie expiration date.

myCookie.Expires = now.AddYears(50); // For a cookie to effectively never expire

// Add the cookie.


Response.Write("<p> The cookie has been written.");

Reading a cookie

HttpCookie myCookie = new HttpCookie("MyTestCookie");

myCookie = Request.Cookies["MyTestCookie"];

// Read the cookie information and display it.

if (myCookie != null)

   Response.Write("<p>"+ myCookie.Name + "<p>"+ myCookie.Value);


   Response.Write("not found");

Don't want to miss updates? Please click the below button!

Follow MindStick