Click Here To Get Amazing Facebook Profile Covers,Status Messages

Cookies for session management in ASP.NET

Cookies are used for storing a piece of information on client machines by web sites.Web developers use these cookies for managing user sessions.
In ASP.NET cookies can be created using class HttpCookie in System.Web namespace.

Cookies are added to the Cookies collection of response object, the object which is responsible for keeping information to be rendered to browser. Example for writing and reading cookie

Create an instance of HttpCookie

Dim mycookie as new HttpCookie("userid")

Set parameters like value and expires

mycookie.Value = "1"

mycookie.Expires = DateTime.Now.AddMonths(1)

Add to the Cookies collection of response object

Response.Cookies.Add(mycookie)

We can do same thing by directly setting values for Response.Cookies collection

Response.Cookies ("userid").value = "1"

Response.Cookies ("userid").Expires = DateTime.Now.AddMonths(1)

The above code creates cookie and store it in user's drive for one month. The following code is used to retrieve a cookie later.

Request object is used to send cookies information along with the request for page to server.

If Not Requet.Cookies("userid") Is Nothing Then

Dim a As String

a= Not Requet.Cookies("userid")

End If

Here first we are checking whether cookie exists or not because it can be deleted by user. Cookies won't work if user has disabled cookies in browser settings.

Happy Coding.

Your rating: None Average: 5 (1 vote)