Sunday, 25 August 2013

Session Helper Class (Facade)

                                                         
Code:

 using System.Web;
/// <summary>
/// Static Session facade class
/// </summary>
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
/// <summary>
/// Clears Session
/// </summary>
public static void ClearSession()
{
HttpContext.Current.Session.Clear();
}
/// <summary>
/// Abandons Session
/// </summary>
public static void Abandon()
{
ClearSession();
HttpContext.Current.Session.Abandon();
}
#endregion
#region Public Static Properties
/// <summary>
/// Gets/Sets Session for UserId
/// </summary>
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();




Share/Bookmark