title: custom controllerfactory: httpcontext.current.user and httpcontext.current.session are null only on ajax post
body: <p>I've implemented a custom controller factory in ASP MVC3 with autofac to achieve a very basic plugin management by deriving from DefaultControllerFactory.</p>
  
  <p>In the overrided GetControllerType(RequestContext requestContext, string controllerName) method I do some checks with a session variable or httpcontext.current.user to determine which controller type I must provide to an end user.</p>
  
  <p>The point is that if I do a normal GET (or by using a childaction inside a view) I have those two set when GetControllerType is called but if I do a POST ajax call (with contentType:'application/json; charset=utf-8') via jquery, directly to the actionmethod, the user and the session are null in my httpcontext but they are set an instant later, when I try to access them in my controller action (or in an actionfilter).</p>
  
  <p>Also the AcquireRequestState in this case is never fired and if I navigate through RequestContext property I can see that cookies for session (with session id) are valid.</p>
  
  <p>Do you have any hint?? And why on normal GET calls session and user are set at that time and I dont have this problem?</p>
  
  <p>The code is like this:</p>
  
  <pre><code>protected override Type GetControllerType(RequestContext requestContext, string controllerName)
  {
      if(plugins.All.Where(w=&gt; w.Name == HttpContext.Current.Session["pluginName"].ToString() &amp;&amp; w.User == HttpContext.Current.User.Identity.Name) 
          [do stuff]
  }
  </code></pre>
  
  <p>I can avoid Session but I cannot avoid matching for the current user.</p>