|
|
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.
- Article Category:
Similar entries
- what are persistent cookies and how they are different from temporary cookies
- enable disable cookies in internet explorer
- enable disable cookies in firefox 3.x
- Internet cookies and why and how websites make use of it
- Howto use QueryString in ASP.NET for passing info between pages.
- Privacy Policy
- solution to Access to the path is denied asp.net
- Data Source Name(DSN) Connection to database in ASP using DSN
- How to resolve "Failed to generate a user instance of SQL Server due to failure in
- configuration file for asp.net web application web.config