SESSION MANAGEMENT QUESTION

Hey Guys, I hope that you are enjoying with my blogs writing and today's we will study "SESSION MANAGEMENT in ASP.NET".                                           

Q. Why http protocol is Stateless?

ANS: Because HTTP(Hyper text transfer protocol) has HTTP cookies, Which allows server to track the user state, number of connection, last connection etc. Browser Cookies often called http cookies or simply cookies are small snippets of data or code that are stored on web browser when a user visits a website.

It is called stateless because in this each command executed independently. A client request some information and the server honored. In the next minutes, the client requested the server to send back the same information as it lost the data. 

             To Understand this concept see the given diagram

Suppose we have a variable ‘x’ in the application. If user1 requests to increase value of x with ‘x++’ then server will increase the value of x and return it to the browser of user1. Now, after some time if user1 again sends the same request to increase value of x to the server, but this time it does not consider that request from same user is occurred before and we have executed it. Server again initialize it as x=0 and increase it to x=1, not x=2..

The Reason behind that, Whenever the user request to the server it creates a separate and temporary memory block for it within the server after sending the response to the user it remove this memory. Therefore the same user request to the same data then it will initialize again.  

Input: In this code 'i' has taken instead of 'x' 

Q. What are Cookies and what are session variables ?

ANS: Cookies are nothing but the temporary storage created in the browser. Session uses cookies to store data in it to again retrieve it within the session when required. It eliminates load of data from server and every user own browser stores data on their side using cookies you can see the cookies in browser by pressing 'F12' select application.


Session Variable:

Session variables are stored in a SessionStateItemCollection  object that is exposed through the HttpContext.Session property. In an ASP.NET page, the current session variables are exposed through the Session property of the Page object. 


 

We can use previous session example where we have created a cookie named ‘cookie i’ in Session. This cookie holds value of i in hashed format for security reasons. Whenever user refreshes the page value of i is updated based on the previous value stored in cookie and passed on to server for further execution and so on. Cookie works normally until the session expires and value is set to the one that is initialized earlier.

Q. How to make it stateful in MVC core?

 ANS: In order to make HTTP stateful , we use session management techniques.

The session management techniques are:

  1. hidden form field
  2. cookie
  3. session
  4. URL-rewriting

Q. How to enable session in MVC core?

Session stores the data in the dictionary on the Server and SessionId is used as a key. The SessionId is stored on the client at cookie. The SessionId cookie is sent with every request.

At the Server end, session is retained for a limited time. The default session timeout at the Server is 20 minutes but it is configurable.


                                                           Thank you...




ليست هناك تعليقات:

إرسال تعليق