Tuesday 18 November 2014

How To Use Cookies ASP.NET.

Cookies provide the ability to store small amounts of data on a clients machine. It is often used to store user preferences, session variables, or identity. When the user visits your Web site another time, the application can retrieve the information it stored earlier.
The browser is responsible for managing cookies on a user system. Cookies can be created, accessed, and modified directly by script running on the client and pass between the client and server during requests.
Writing code in C#
Response.Cookies["UserName"].Value = "Ashok";
Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(1);
Reading code in C#
if(Request.Cookies["UserName"] != null)
Label1.Text = Server.HtmlEncode(Request.Cookies["UserName"].Value);

Cookies can be either temporary .Temporary cookies are stored in the clients browser. These cookies are saved only while your web browser is running.on the other hand, are stored on the hard disk of the client computer as a text file. They stay on your hard disk and can be accessed by web servers until they are deleted or have expired. These cookies can be retrieved by the Web application when the client establishes another session with it.
Cookies are limited to 4096 bytes in size and are only capable of storing strings. Browsers also impose limitations on how many cookies your site can store on the users computer. Most browsers allow only 20 cookies per site; if you try to store more, the oldest cookies are discarded.

0 comments::

Post a Comment

Copyright © DotNet-Ashok