A cookie is just a little bit of text which is send by the browser each time with the request for the page. Each time on the machine when browser will make the request to the web page, it will send the existing cookies along with the request too. So this approach helping the Web site or web application remember users and other information such as user’s preferences, shopping cart contents and the user’s session.
Let’s suppose that if you authorize the web site to remember the user name and password then that web site writes the current user information in the form of the cookie and when user will return to this web site again, the website can identify the user and continue his session.
There are several ways to create cookies, such as
You can write the cookies by using HttpCookies class:
HttpCookie userinfo = new HttpCookie("userinfo"); userinfo.Value = TextBox1.Text; userinfo.Expires = DateTime.Now.AddHours(1); Response.Cookies.Add(userinfo);
or you can write the cookies directly without using the HttpCookies class:
Response.Cookies["userinfo"].Value = TextBox1.Text; Response.Cookies["userinfo"].Expires = DateTime.Now.AddDays(1);
How to Read Cookies
It is easy to read the saved cookies like as:
string username = Request.Cookies["userinfo"].Value
Because different browsers store cookies differently, different browsers on the same computer won’t necessarily be able to read each other’s cookies. For example, if you use Internet Explorer to test a page one time, but then later use a different browser to test again, the second browser won’t find the cookies saved by Internet Explorer.
Write the sub keys in single cookie
The following code shows that how you can write the single cookie with two subkeys:
Response.Cookies["userinfo"]["name"].Value = TextBox1.Text; Response.Cookies["userinfo"]["email"].Expires = DateTime.Now.AddDays(1); Response.Cookies["userinfo"].Expires = DateTime.Now.AddDays(1);
Things to beware before using the cookies
Generally most of browsers can support cookies of up to 4096 bytes(4kb). So we should use the cookies for only the small amounts of data.
Most browsers allow only 20 cookies per site, if you try to write more, the oldest cookies will be deleted. Number of the cookies per site is depends on the browser to browser so don’t try to write more then 20 cookies for the one website.
Cookies can be read by external programs or persons so don’t store sensitive information in cookies.
See the existing cookies in Chrome browser
1. Open the Crome browser
2. write the chrome://settings/cookies in the address bar and press enter or navigate the address chrome://settings/cookies.
3. window will display with all the cookies data.
See the existing cookies in Mozilla Firefox browser
1. Goto Tools > Page Info.
2. Click on the ‘Security’ tab.
3. Click on ‘View Cookies’ button.
See the existing cookies in Internet-Explorer browser
To view cookies in Internet Explorer, open the Temporary Internet Files folder in your hard drive.
1. Open your IE browser.
2. Click “Tools” then select “Internet Options.”
3. Click “Settings” under Browsing History.
4. Click “View Files” to view Temporary Internet Files.
Please comment your feedback.