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:
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:
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.
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?
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.
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
}
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;
}
}
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();
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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.
ASKER
We have decided to use SQL server to maintain out-of-process session state.
ASKER