Solved

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

Posted on 2014-10-29
5
524 Views
Last Modified: 2014-12-10
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?
0
Comment
Question by:dyarosh
  • 3
5 Comments
 
LVL 78

Accepted Solution

by:
David Johnson, CD, MVP earned 500 total points
ID: 40412493
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
 

Author Comment

by:dyarosh
ID: 40413200
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
 
LVL 25

Expert Comment

by:apeter
ID: 40414466
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
 

Author Comment

by:dyarosh
ID: 40435388
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
 

Author Closing Comment

by:dyarosh
ID: 40491679
We have decided to use SQL server to maintain out-of-process session state.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now