Session Helper Class (Facade)


                                                         
Code:
 using System.Web;
///

/// Static Session facade class
///

public static class SessionHelper
{
# region Private Constants
private const string userId = “UserId”;
private const string projectId = “ProjectId”;
# endregion
#region Private Static Member Variables
private static HttpContext thisContext;
#endregion
#region Public Static Methods
///

/// Clears Session
///

public static void ClearSession()
{
HttpContext.Current.Session.Clear();
}
///

/// Abandons Session
///

public static void Abandon()
{
ClearSession();
HttpContext.Current.Session.Abandon();
}
#endregion
#region Public Static Properties
///

/// Gets/Sets Session for UserId
///

public static string UserId
{
get
{
if (HttpContext.Current.Session[userId] == null)
return “”;
else
return HttpContext.Current.Session[userId].ToString();
}
set { HttpContext.Current.Session[userId] = value; }
}
public static string ProjectId
{
get
{
if (HttpContext.Current.Session[projectId] == null)
return “”;
else
return HttpContext.Current.Session[projectId].ToString();
}
set { HttpContext.Current.Session[projectId] = value; }
}
#endregion
}
//Use as: SessionHelper.UserId=”user1″;
//        string user=SessionHelper.UserId;
//        SessionHelper.Abandon();

Advertisements

Author: Akhil Mittal

Akhil Mittal is a Microsoft MVP(Most Valuable Professional), C# Corner MVP, Codeproject MVP, a blogger, author and likes to write/read technical articles. Akhil has an experience of around 11 years in developing, designing, architecting enterprises level applications primarily in Microsoft Technologies. Akhil enjoys working on technologies like MVC, Web API, Entity Framework, Angular, C# and BlockChain. Akhil is an MCP( Microsoft Certified Professional) in Web Applications (MCTS-70-528, MCTS-70-515) and .Net Framework 2.0 (MCTS-70-536). Visit Akhil Mittal’s personal blog CodeTeddy (https://codeteddy.com) for some good and informative articles.