Cookie, Session and Token


Browsing is significant in everyday life and unavoidable. Do you know how your data is stored and how you are getting the responses based on your HTTP request through the web browser?

A session is created temporarily when the user wants to use a particular website. It refers to the user's visiting time and has other session-related details. The cookie and session are combined together to work for the user's better experience.

The web browser sends the session ID with the cookie to the server to check the user is valid when they try to access another related page and the session ID is unique and hard to guess. The session information will be stored in the server's database only when the cookie has the session ID details.
  • In the bank example, when the user logs in to their account, the session is created with ID and the session ends when the user logs out or is inactive for some time.
A token is totally a different concept in which the server creates a token ID when the user wishes to establish a connection with the server through the Internet.

For example, the user downloads a bank application to use his/her account. The token ID will be generated after entering user credentials and it is used for the future login without providing credentials again. Some of the commonly used tokens are OAuth, OpenID, and JWT tokens.

A term called 'Cookie' stores your data in the webserver to provide a better user experience.

The term Cookie was coined by Lou Montulli. A cookie contains a unique identifier for each session called Session ID. It carries the Session ID with other key-value pairs and other relevant information.

When we take bank login as an example, 
  • The user tries to log in with his/her credentials.
  • After providing the credentials and entering into the user homepage, a unique identifier will be generated by the web server, and it is stored temporarily or permanently based on the session availability.
  • The cookie will be deleted permanently when the session is closed by closing the browser or the user inactivates for a few minutes.
Cookie has privacy concerns based on user preferences. Ultimately, the cookie helps us to give better performance and usability while using the website.
set-cookie is the HTTP header for every HTTP request and the server returns the cookie with the HTTP response when it is used for future purposes. The cookie will be available until the session is active. 

On the whole, 
  • You can view all the information with the Session ID,  but with the token ID, you can view certain information. The token is helpful when the third party enters between the client and the server. All the user information is cryptographically signed and it is a temporary password.
  • Session and Cookie are between a client and a server. Token involves typically multiple parties and may not trust each other. 
  • Session and Cookie do not follow any standard rather Token follows a standard to ensure interoperability.
  • A cookie is mentioned in the 'Cookie' HTTP header and the token is in the 'Authorization' HTTP header.
Session-based and token-based authentication can be used in parallel. It can be seen in the bank example. The session-based approach will be used in the web browser when the user accesses his/her bank account and the token-based approach will be used when the user tries to access bank details through the mobile application.



Comments

Popular posts from this blog

How to Install Jenkins

HTTP Payloads

Variables in Postman