|
.Net framework includes built-in support for automatically tracking and associating resources on the server with users. When a user first requests a page from a asp.net web site, the site automatically associate a session cookie to the user browser. The cookie named as “_asp.net_sessionId” cookie, this will tracks the user visit to the web site. Session is associated to the user.Session can stored any type of object. Each user session is assigned a unique session ID.The user can retrieve the sessionID by using the session Object. Eg. Response.Write(Session.SessionID)
Adding Item to Session State
The session attributes can be managed y the following code:-
Session(“sessionItem”) = “Session variable is set here” // This will set the value in the session element.
Response.write(Session(“sessionItem”)) // This will display the value contained in the session element.
Session.Remove(“sessionItem”) // this will remove an item from the session state
Session.RemoveAll() // This will remove all the items stored in the session state for a particular user.
Session.Abandon
This will kill all the session object that are associated to the user. If a user request a new page after the Abandon method, the site treats him/her as a new user.
There are 3 ways for storing session state variables
1. Inproc: By default the session variables are stored in process i.e within the application. The advantage of storing the session state in process is performance. Here you can store and retrieve data much faster because you donot need to marshal the data across separate process.
2. StateServer: Instead of storing the data on the same server, you can store it on separate window service. You can implement this by modifying the Web.Config configuration file.
<configuration>
<system.web>
<sessionstate mode=”StateServer” stateConnectionstring=”tcpip=127.0.0.1:42424” />
</system.web>
</configuration>
3. SQLServer: The other way to store session state out of the process by storing session data in a Microsoft Sql server database. You cn implement this by changing Web.Config configuration file as follows :-
<configuration>
<system.web>
<sessionstate mode=”SQLServer” sqlConnectionstring=”datasource=127.0.0.1; UID=”” pwd=”” “/>
</system.web>
</configuration>
|