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);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s