Session State not persisting between Asp.Net WebForm application and Asp.Net MVC Application

I have an interesting problem that I will try and describe with hopes that someone can help me.

I work in a small group that develops INTRANET applications for our company.  We work in ASP.NET and have applications that are WebForms and MVC 4.  We have an authentication process that captures the user's NTID from HttpContext.User.Identity.Name.  Using that value, the user is looked up in a database and if found the user is authenticated and values from the database are retrieved and stored in Session variables that are then used by the INTRANET applications.

This approach works well for the WebForms applications.  The Session variables are available to the WebForm applications.  The Session variables are not available to the MVC application and I'm not sure why.

Here is the code that is used to set the Session Variables:
        public void SetSessionVars(string NTID)
        {


            string connectionString = GetConnectionString("ORACLE");

            EMPDatabase EMPData = new EMPDatabase(connectionString);
            if (EMPData.ErrorMessage != "")
            {
                return;
            }
            DataTable EmployeeLoginTBL = new DataTable();
            EmployeeLoginTBL = EMPData.EmployeeProfileLogin(EMPData.connection, NTID);

            if (EmployeeLoginTBL == null)
            {
                return;
            }

            if (EmployeeLoginTBL.Rows.Count == 0)
            {
                return;
            }

            DataRow dr = EmployeeLoginTBL.Rows[0];

            HttpContext.Current.Session["SupEmail"] = EMPData.GetSMDEmail((decimal)dr["EmployeeID"]);
            HttpContext.Current.Session["MgrEmail"] = EMPData.GetSMDEmail((decimal)dr["EmployeeID"], "MGR");
            HttpContext.Current.Session["DirEmail"] = EMPData.GetSMDEmail((decimal)dr["EmployeeID"], "DIR");


            HttpContext.Current.Session["HeadCountID"] = dr["EmployeeID"].ToString();
            HttpContext.Current.Session["EmployeeProfileID"] = dr["EmployeeID"].ToString();
            HttpContext.Current.Session["FirstName"] = dr["FirstName"].ToString();

            if (string.IsNullOrWhiteSpace(dr["NickName"].ToString()) || string.IsNullOrEmpty(dr["NickName"].ToString()))
                HttpContext.Current.Session["NickName"] = dr["FirstName"].ToString();
            else
                HttpContext.Current.Session["NickName"] = dr["NickName"].ToString();

            HttpContext.Current.Session["LastName"] = dr["LastName"].ToString();
            HttpContext.Current.Session["EmailAddress"] = dr["EmailAddress"].ToString();
            HttpContext.Current.Session["NTID"] = NTID;
            HttpContext.Current.Session["userid"] = NTID;

           // Removed the rest of the list of session variables being set 
        }

Open in new window


This method is in a DLL that is called from our Authentication application.

Now we have an application that lets us display the session variables that is a WebForms application.  When run, the Session variables set during Authentication are displayed.  Here is the code:

        protected void Page_Load(object sender, EventArgs e)
        {
            tbSessionVariables.Text = "HttpContext.Current.User.Identity.Name = " + HttpContext.Current.User.Identity.Name + Environment.NewLine;
            foreach (string key in Session.Keys)
            {
                tbSessionVariables.Text = tbSessionVariables.Text + key + " - " + Session[key] + Environment.NewLine;
            }
        }

Open in new window


When I go to a controller in my MVC application to display the Session Variables, no session variables are found.  I added a Session variable before I try and display to make sure that something will be returned.  With the following code I only see the Session variable that I added in the MVC application.
        public ActionResult Index()
        {
            string s = null;
            Session["Test"] = "test";
            foreach (string key in HttpContext.Session.Keys)
            {
                s += key + " - " + HttpContext.Session[key] + Environment.NewLine;
            }

            ViewBag.Session = s;
            return View();
        }

Open in new window


I checked on the server and the Session State for both applications is set to In Process.  I also verified that both applications have the same Application Pool.

What do I need to do to be able to access the Session variables in the MVC 4 application?
dyaroshAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
use another method of storing the session state i.e. state server or sql database rather than inproc
http://msdn.microsoft.com/en-us/library/ms178586%28v=vs.100%29.aspx
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dyaroshAuthor Commented:
We are looking into using a different session state management but need to make sure none of our Apps use the Session_OnEnd event.  I'm just wondering why the problem is occurring since we don't use a web farm and the applications all use the same Application Pool.
0
apeterCommented:
Maybe this will help. You have to remove and add session in the config as mentioned here, http://forums.asp.net/t/1696851.aspx?MVC+sharing+Session+state+with+WebForms+on+IIS+vs+Cassini
0
dyaroshAuthor Commented:
Thanks for the link.  I am looking into it.  I tried to do it as it was done in the link but I got an error.  I need to do a little more digging because we are using MVC4 and the link references MVC2.  I'll let you know if it works for us.
0
dyaroshAuthor Commented:
We have decided to use SQL server to maintain out-of-process session state.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.