Creating a SessionWrapper in ASP.NET

A nice way to strongly type your session. Create a class with accessible properties allowing you to easily handle session manipulation through an object.

using System;
using System.Collections.Generic;
using System.Web;
using com.froyo.ehap.bl;

namespace com.froyo.ehap.ui
{
    /// <summary>
    /// This class is a store for the Application-wide contexts.
    /// Properties on this class are accessible application-wide, and are valid across the application.
    /// </summary>
    public static class SessionWrapper
    {

        /// <summary>
        /// Gets or sets the current logged in user.
        /// </summary>
        /// <value>The current logged in user.</value>
        public static EHAPUser CurrentLoggedInUser
        {
            get { return GetFromSession<EHAPUser>("EHAPUser"); }
            set { SetInSession<EHAPUser>("EHAPUser", value); }
        }



        /// <summary>
        /// Gets from session.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        private static T GetFromSession<T>(string key)
        {
            object obj = HttpContext.Current.Session[key];

            if (obj == null)
                return default(T);

            return (T)obj;
        }

        /// <summary>
        /// Sets the in session.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        private static void SetInSession<T>(string key, T value)
        {
            if (value == null)
                HttpContext.Current.Session.Remove(key);
            else
                HttpContext.Current.Session[key] = value;
        }

        /// <summary>
        /// Gets from application.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        private static T GetFromApplication<T>(string key)
        {
            return (T)HttpContext.Current.Application[key];
        }

        /// <summary>
        /// Sets the in application.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        private static void SetInApplication<T>(string key, T value)
        {
            if (value == null)
                HttpContext.Current.Application.Remove(key);
            else
                HttpContext.Current.Application[key] = value;
        }

        /// <summary>
        /// Handles a redirect based upon an expired session
        /// </summary>
        public static void SessionExpiredRedirect()
        {
            // The user's session has expired. Redirect back to the Default home page.
            System.Web.HttpContext.Current.Response.Redirect("/SiteSessionExpired.aspx", false);
            System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest();
        }
    }
}

Example of it’s usage

 /// <summary>
        /// Handles the Start event of the Session control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Session_Start(object sender, EventArgs e)
        {          

            SessionWrapper.CurrentLoggedInUser = UserServiceManager.GetEHAPUser(HttpContext.Current.User.Identity.Name.Remove(0, 5));
        }

Another example of usage:

   bool isAssignedAnalyst = SessionWrapper.CurrentLoggedInUser.Equals(JHA.Analyst);

Example Using Session_Start in Global.asax

In the Global.asax.cs of your ASP.NET site:

Using the current windows authenticated username pass this to our webservice which will pass us back info about this user which we can use in our system

 /// <summary>
        /// Handles the Start event of the Session control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Session_Start(object sender, EventArgs e)
        {
            Session["EhapUser"] = UserServiceManager.GetEHAPUser(HttpContext.Current.User.Identity.Name.Remove(0, 5));
        }

Then in your MasterPage code behind:

Reference the object that is now in session

       protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                EHAPUser user = (EHAPUser)Session["EhapUser"];
                if(user == null)
                    lblLogon.Text = HttpContext.Current.User.Identity.Name;
                else
                    lblLogon.Text = user.FullName;
                lblApplicationVersion.Text = ConfigurationManager.AppSettings["applicationVersion"];
            }
            ErrorMessage = null;
        }