Introduction
For a project of my former employer, I�ve run into a problem of losing Session information after communication with other applications on different domains. My ASP.NET application was communicating via hidden-fields through a POST on a Java-Servlet and PHP pages. These pages get my postback-aspx-URL but after the postback, I lose the Session information because these applications where running on a different domain.
That's why I've looked for the possibilities in ASP.NET for storing Session information, without using difficult State-Management-servers or databases.
Creating the framework
ASP.NET developers are familiar with the objects Session
and Cache
, these objects are central in my framework for managing Session information.
I�ve started creating an object for storing Session information, named SessionObject
. See listing 1. In this object, you can place your variables needed for your sessions.
public class SessionObject
{
private string _userName = string.Empty;
public string UserName
{
get { return _userName; }
set { _userName = value; }
}
}
Listing 1. SessionObject.cs
Thereafter, I�ve been thinking how I can get SessionObject
back. In ASP.NET there is an object called Cache
. This object is created on each Application-Domain and is type-safe on multi-threaded applications. Don�t forget that when your ASP.NET application is unloaded by the WebServer, your Cache
object is also flushed. See listing 2.
public class SessionManager
{
private const string SESSION_MANAGER = "SessionManager";
private HttpContext _context;
private Guid _sessionID;
private SessionObject _sessionObject;
public SessionManager (HttpContext httpContext)
{
_context = httpContext;
}
public static void Initialize(HttpContext httpContext)
{
httpContext.Items.Add(SESSION_MANAGER, new SessionManager (httpContext));
}
public static SessionManager Current
{
get { return (SessionManager) HttpContext.Current.Items[SESSION_MANAGER]; }
}
public Guid SessionID
{
get { return _sessionID; }
set
{
if (!_sessionID.Equals(value))
{
_sessionID = value;
GetSessionObject(value);
}
}
}
private void GetSessionObject(Guid sessionID)
{
_sessionObject = _context.Cache[sessionID.ToString()] as SessionObject;
if (_sessionObject == null)
SetSessionObject(sessionID, new SessionObject());
}
private void SetSessionObject(Guid sessionID, SessionObject sessionObject)
{
_context.Cache.Add(sessionID.ToString(),
sessionObject,
null,
DateTime.MaxValue,
TimeSpan.FromHours(2),
System.Web.Caching.CacheItemPriority.High,
null);
_sessionObject = sessionObject;
}
public SessionObject SessionObject
{
get { return _sessionObject; }
}
}
Listing 2. SessionManager.cs
The SessionManager
uses a SessionID
, this is stored in the Session
object (not our SessionObject
) or it will be returned by a QueryString
variable of ASPX-page. On every request on the ASP.NET application, the SessionManager
is initialized and added on the HttpContext.Items
.
protected void Application_BeginRequest(Object sender, EventArgs e)
{
Chompff.SessionManager.Web.SessionManager.Initialize(Context);
}
Listing 3. Global.asax.cs
By creating a base ASPX-page where all other ASPX-pages derive from, with a property SessionManager
, you have access to the SessionManager
on all other ASPX-pages. But it�s possible that the SessionManager
doesn�t have a SessionObject
initialized. Therefore, we create a property SessionID
, and by setting the SessionID
of the base ASPX-page, the SessionManager
gets the SessionObject
from the Cache, or when it doesn�t exist, it creates one and places it in the Cache. See listing 4.
public class BaseWebForm : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
}
public SessionManager SessionManager
{
get { return SessionManager.Current; }
}
public Guid SessionID
{
get { return (Guid) Session["SessionID"]; }
set
{
Session["SessionID"] = value;
SessionManager.SessionID = value;
}
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
}
Listing 4. BaseWebForm.cs
In every ASP.NET application, there is a start page. This page has to create a new SessionID
by executing in the Page_Load
: this.SessionID = Guid.NewGuid;
. Now, there is a SessionObject
created in the Cache, and you can get the Session information on every other ASPX-page by setting SessionID
on that page.