Session management is a very common technique in web application development. We know that it's one of the most secure ways to exchange data because session data passes in an encrypted format from the client to the server and it's very secure.
In spite of the advantages, sessions may cause a few bottlenecks of applications, like session data stored in server memory and passed through every request and response. Now, if there is a huge number of session variable in the application then it might result in a shortage of server memory when a huge number of clients access the application.
And another great problem that may occur in a session is the key identity problem. For example, a project might have many modules and each module is being developed by separate developers, so there is the chance of two developers using the same key to store a session variable and after integration of modules it will be a big problem, or sometimes there might be a limitation of session variables in an application.
So, those are scenarios where a session might create a problem for both an application and a developer. But, don't lose hope. There is a solution for that. We can centralize session handling in the application. Have a look at the following code.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace WebAPP
- {
- public class CentralSession
- {
- public string UserId
- {
- get
- {
- return HttpContext.Current.Session["User"] == null ?
- null :HttpContext.Current.Session["User"].ToString();
- }
- set
- {
- HttpContext.Current.Session["User"] = value;
- }
- }
- public int ItemInCart
- {
- get
- {
- return HttpContext.Current.Session["Items"]== null ?
- 0 :Convert.ToInt32( HttpContext.Current.Session["Items"]);
- }
- set
- {
- HttpContext.Current.Session["Items"] = value;
- }
- }
- public Object SessionMain
- {
- get
- {
- return HttpContext.Current.Session["MasterUser"] == null ?
- null : HttpContext.Current.Session["MasterUser"];
- }
- set
- {
- HttpContext.Current.Session["MasterUser"] = value;
- }
- }
- }
- }

Join the conversation! Your thoughts help the community grow.