Tuesday, 30 July 2013

Difference between Cookies and Session in ASP.Net || What is cookie? || What is session? How to use sessions and cookies?

Hi Friends, in this article I will explain about the Difference between Cookies and Session in Asp.Net.
I already explained in the previous articles about what is cookie? How to use cookie? What is session? How to use sessions? and i gave one Session State example in ASP.NET Session State Example in C#, VB.NET.and one cookie example in Example for cookie.
We can use session and cookies to maintain the state while during page postbacks.

We can maintain state in two ways.
1. Client Side (Like hidden fields, query string, view state and Cookies)
2. Server Side (like Session and Application)
The below figure shows about how  Session Id created.


Differences:
  • Cookies store the data in client side either in web browser or in system hard disk. Session allocates memory in the web server and for every user.
  • Any data stored in a session variable is destroyed when the session ends (i.e. when the user logs off or closes the browser) but any data stored in a cookie will survive.
  • Cookies do not require any server resources. Session required server resources.
  • We can store small data in cookies but session object stores large amount of data. Because the size of cookies are very small.
  • Cookies are in a string format. But you can store any object like DataTable , ArrayList etc. in a Session.
  • Users can manipulate cookie. But session will not be manipulated.
  • Most of the browsers allow only 20 cookies per site. Session is used to Store values across other pages of the same web site.
  • Compared to cookies, sessions are more secure.
  • Cookie expires at specified date by you. After user closes browser, session timeout clears all information. (Default is 20 minutes)
  • There are two types of cookies persistent and non-persistent cookies. Sessions are called as Non-Persistent cookies because its life time can be set manually.
  • We can disable cookies. We cannot disable the sessions. Sessions can be used without cookies also (by disabling cookies).
  • Only in few situations we can use cookies because of security. But we can use sessions in all situations.
  • Cookie is non-secure since stored in text format at client browser .But Session are secured because it is stored in binary format/encrypted form and it gets decrypted at server.
Go to previous interview question Difference between Structure and Class in .NET || What are the difference between Structure and Class in .NET?
I think you like my blog Aspdotnet-Roja why are waiting following me on facebook fan page Aspdotnet-roja

No comments:

Post a Comment

© 2012-2014 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.