Switch to full style
ASP Technology Tutorials Written By Members.
Post a reply

Using cookies in ASP

Mon May 21, 2012 11:49 pm

This article about using cookies in ASP, cookies is a famous concept for web developers, cookie is to keep information about website visitor at the client side, and the visitor can disable creating cookies from the browser configuration. There are many examples of using cookies , the most famous one is “Remember Me” in case of user login , that is mean when the user reopen the website page in a later time he will be automatically login to the system using the username and password save at his computer. Storing information at client side is useful by saving web server resources especially in cases of websites that have a big volume of daily visitors.
In ASP you can creating cookie using response object which take effect on HTTP header, in following example we store to piece of information:
Code:
Response.Cookies("Name")=" Name "
Response.Cookies("Password")= "Password"


This is an example of ASP (Active Server Pages) code that demonstrates how to create and send cookies to the client's browser. Cookies are small text files that are stored on the client's computer by the browser, they are used to store information that the server can later retrieve, such as user preferences or login information. The first line of the code creates a cookie named "Name" and assigns a value of "Name" to it. The second line creates a cookie named "Password" and assigns a value of "Password" to it. Cookies are created and sent to the client's browser using the "Response.Cookies" collection, which is a built-in object in ASP. The "Response.Cookies" collection is used to create new cookies and set their values. It's important to note that cookies are sent to the client's browser via the HTTP headers, therefore, the cookies must be set before any output is sent to the client, otherwise, they will not be sent.
Cookies are sent to the client's browser with every request, the browser will automatically include the cookies in the headers of the request, allowing the server to read the values stored in the cookies. It's worth noting that cookies can also be set to expire after a certain period of time, and also can be set to be accessible to only specific pages or directories, this feature is useful to increase security and also to manage the data that is stored in cookies.
The script is an example of how to create and send cookies to the client's browser using the "Response.Cookies" collection in ASP. It's important to handle the security of the data that is stored in cookies, also it's important to be aware of the data that is stored in cookies and the duration of the expiration time.



We can also using keys in saving cookies like this:
Code:
Response.Cookies("Codemiles") ("Name")=" Name "
Response.Cookies("Codemiles") ("Password")= "Password"

Important information about cookies is the expiry date, if we didn’t specify expiry date to or cookie it will be expired once the browser is closed or the current session is terminated.
Code:
Response.Cookies("Name").Expires = Date +7
Response
.Cookies("Password ").Expires = Date +7

The above code will set the expired date to one week in advance, now the question is how we will get information in these cookies? Actually it is pretty easy as follows:
Code:
Response.Write(Request.Cookies("Name")) 
Response
.Write(Request.Cookies("Password "))

Or in case of keys:
Code:
Response.Write(Request.Cookies("Codemiles") ("Name")) 
Response
.Write(Request.Cookies("Codemiles") ("Password "))




Additionally, it's important to consider the privacy implications of using cookies. As cookies store information on the client's computer, they can potentially be accessed by malicious actors. Therefore, it's important to ensure that sensitive information, such as passwords, is properly encrypted before being stored in a cookie. Cookies can also be used for tracking user behavior, which can be a privacy concern. It's important to be transparent about the usage of cookies and to provide users with the option to opt out of cookie tracking. It's also worth noting that some browsers and devices have limitations on the number and size of cookies that can be stored. It's important to keep the number and size of cookies small to ensure that they can be stored on all devices. Another important aspect to consider is that, if the cookies are not properly secured, an attacker could steal the cookies and impersonate the user, this is known as a "Session Hijacking" attack. To prevent this, cookies can be secured by adding "Secure" and "HttpOnly" attributes. It's important to consider the security and privacy implications when using cookies and to handle them properly to ensure that sensitive information is protected and that user privacy is respected. It's also important to be aware of the limitations of cookies in terms of storage and to ensure that cookies are properly secured to prevent session hijacking attacks.



In recent years, alternative solutions to cookies have been developed such as browser storage APIs like Local Storage, Session Storage, and IndexedDB. These alternatives provide similar functionality to cookies but with the added benefits of larger storage capacity and better security features. However, it's important to keep in mind that these technologies are only available in modern browsers and may not be supported on older or less capable devices. Another alternative is the use of JSON Web Tokens (JWT) to authenticate the user and store information on the client side. JWT is a standard that defines a compact and self-contained way to securely transmit information between parties. JWT is a more secure way to store user information on the client side, but it requires a more complex implementation. In conclusion, it's important to consider the different options available for storing information on the client side and to choose the one that best suits the requirements of the application and the target audience. And also, it's important to keep in mind the security and privacy implications of storing information on the client side, and handling that information properly.



Post a reply
  Related Posts  to : Using cookies in ASP
 cookies &rewriting URL     -  
 Handle Cookies using JQuery     -  

Topic Tags

ASP Cookies