---
title: "How to store string in a cookie and retrieve it"  
description: "How to store string in a cookie and retrieve it"  
author: "Takeshi Okada"  
published: 2014-08-25  
updated: 2014-08-25  
canonical: https://www.mindstick.com/forum/2135/how-to-store-string-in-a-cookie-and-retrieve-it  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 1 minute  

---

# How to store string in a cookie and retrieve it

I want to [store](https://www.mindstick.com/articles/13125/tips-to-increase-sales-of-your-woocommerce-store) the username in the [cookie](https://www.mindstick.com/blog/123/what-is-cookie-poisoning) and [retrieve](https://www.mindstick.com/forum/34400/how-to-retrieve-form-values-in-controller-action) it the next time when the user opens the website. Is it possible to create a cookie which doesnt expires when the [browser](https://www.mindstick.com/blog/155254/which-is-the-best-internet-browser) is closed. I am using [asp.net c#](https://www.mindstick.com/forum/12726/how-to-retrieve-file-version-of-assembly-information-window-in-asp-dot-net-c-sharp) to create the website. And how can I stop the browser from [offering](https://answers.mindstick.com/qa/116481/a-complete-look-at-umrah-packages-offering-5-star-comfort) to save [username and password](https://www.mindstick.com/forum/160407/how-does-a-bearer-token-differ-from-other-authentication-methods-such-as-username-and-password)

## Replies

### Reply by Sumit Kesarwani

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.Cookies.Add(myCookie);Response.Write("<p> The cookie has been written.");Reading a cookieHttpCookie 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);else  
Response.Write("not found");
```


---

Original Source: https://www.mindstick.com/forum/2135/how-to-store-string-in-a-cookie-and-retrieve-it

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
