Link to home
Start Free TrialLog in
Avatar of dyarosh
dyarosh

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dyarosh
dyarosh

ASKER

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.
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
Avatar of dyarosh

ASKER

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.
Avatar of dyarosh

ASKER

We have decided to use SQL server to maintain out-of-process session state.