Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The Session object is null in ASP.NET MVC 4 webapplication once deployed to IIS 7 (W 2008 R2)

I developed an ASP.NET MVC 4 web application (.net 4.5) which runs fine in Visual Studio 2012. After deployment to IIS 7 on Windows Server 2008 R2, it seems like the HttpContext.Session object inside my controller is null. I created a simple test ASP.NET MVC 4 application to demonstrate the issue.

In my test app I have a simple Home Controller

public class HomeController : Controller
{
    public ActionResult Index()
    {
          if ( HttpContext != null && HttpContext.Session != null )
          {
              HttpContext.Session[ "test" ] = "a string in session state";
              ViewBag.Info = HttpContext.Session[ "test" ];
              return View();
          }
          else
          {
              if ( HttpContext == null )
              {
                  ViewBag.Info = "beeeeuuuuu - HttpContext = null!!!";
              }
              else if ( HttpContext.Session == null )
              {
                    ViewBag.Info = "beeeeuuuuu - Session = null!!!";
              }
              return View();
          }

    }
}

My Index.chtml view lookes something like this:

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
This is a simple test
<p>@ViewBag.Info</p>

So when I run the application I get what I was expecting:

Index
This is a simple test 
a string in session state

But after I deploy the application to the web server the website gives me the following page indicating that the Session object is null:

Index
This is a simple test 
beeeeuuuuu - Session = null!!!

The web application is deployed to the default website which runs under the ASP.NET v4.0 application pool (integrated pipeline).

I already reïnstalled ASP.NET on the server using aspnet_regiis -ir but this didn't help. The Session state is enabled (In Proc) at the ISS server. I hope anyone can help me out here cause I'm trying to solve this for quite some time.

Much thanks in advance and kind regards.

UPDATE: I also tested an ASP.NET MVC 4 build with .NET 4.0 instead of 4.5 and that has the same problem. I also deployed an ASP.NET Web Pages app (.NET 4.0) and that works fine (The HttpContext.Current.Session is not null in the code behind).

UPDATE II: I also tried to store the Session State in a database, which worked fine on my development machine but had the same problem on the production server (HttpContext.Session still returns null).

like image 466
Gert Avatar asked Nov 28 '22 14:11

Gert


1 Answers

I found the solution. You have to add and remove the SessionStateModule:

  <configuration>
  ...
  <system.webServer>
    ...
    <modules>
      <remove name="Session" />
      <add name="Session" type="System.Web.SessionState.SessionStateModule"/>
      ...
    </modules>
  </system.webServer>
</configuration>

I have no idea why Microsoft doesn't add this to the web.config of the project template?

like image 91
Gert Avatar answered Dec 04 '22 04:12

Gert