Cookies are small text files that are added to a users browser by a web application to store session/user specific data. A web application is able to read the contents of a cookie(s) on each subsequent visit a user makes to a website.
Security considerations
Cookies are unsuitable for storing secure information such as passwords.
Adding a cookie
A cookie may contain a number of name/value pairs when it is added to the browser. The expiry date of a cookie defines how long the cookie should remain in the browser for.
Response.Cookies("userInfo")("userName") = "straximo"
Response.Cookies("userInfo")("lastVisit") = DateTime.Now.ToString()
Response.Cookies("userInfo").Expires = DateTime.Now.AddDays(1)
Use the Response (outgoing) object to add a cookie.Response.Cookies("userInfo")("lastVisit") = DateTime.Now.ToString()
Response.Cookies("userInfo").Expires = DateTime.Now.AddDays(1)
Modifying a cookie
A cookie may be modified simply by overwriting it.
Response.Cookies("userInfo")("userName") = "straximo4"
Response.Cookies("userInfo")("lastVisit") = DateTime.Now.ToString()
Response.Cookies("userInfo").Expires = DateTime.Now.AddDays(1)
Use the Response (outgoing) object to modify a cookie.Response.Cookies("userInfo")("lastVisit") = DateTime.Now.ToString()
Response.Cookies("userInfo").Expires = DateTime.Now.AddDays(1)
Removing a cookie
A cookie is removed by setting the expiry date to a time in the past.
Response.Cookies("userInfo").Expires = DateTime.Now.AddDays(-1)
Use the Response (outgoing) object to remove a cookie.Reading a cookie
After checking that a cookie exists, a cookie value may be read & used just like any other value.
If Not Request.Cookies("userInfo") Is Nothing Then
Label1.Text = Request.Cookies("userName")("userName")
Label2.Text = Request.Cookies("userName")("lastVisit")End If
Label1.Text = Request.Cookies("userName")("userName")
Label2.Text = Request.Cookies("userName")("lastVisit")End If
Use the Request (outgoing) object to read a cookie.
No comments:
Post a Comment