title: Asp.Net mvc - accessing IoC container from Session_Start
body: <p>I want to manage my auth cookies similar to <a href="http://fishbowl.pastiche.org/2004/01/19/persistent_login_cookie_best_practice/" rel="nofollow">http://fishbowl.pastiche.org/2004/01/19/persistent_login_cookie_best_practice/</a></p>
  
  <p>I'd like to check the cookie on session start, auth the user if there is one and exchange it for a new one on the start of each new session. I'd also like to create one if none exist. </p>
  
  <p>This is to take care of the 'remember me' type functionality - similar to how SO works.</p>
  
  <p>To do this I need to be able to pull services from the container from within the Session_Start method in the global.asax. While debugging the app I step through the Application_Start method where the container is constructed. Everything goes OK and the Container property of the global.asax is created. But when I step into the Session_Start - the Container is null. </p>
  
  <p>Is there something happening that I'm not aware of? Is there a better way to be doing this?</p>
  
  <pre><code>protected void Application_Start()
          {
              AreaRegistration.RegisterAllAreas();
  
              RegisterGlobalFilters(GlobalFilters.Filters);
              RegisterRoutes(RouteTable.Routes);
  
              Container = new WindsorContainer().AddFacility&lt;WcfFacility&gt;()
                  .Install(Configuration.FromXmlFile("Configuration\\Windsor.config"))
                  .Install(FromAssembly.InDirectory(new AssemblyFilter(HttpRuntime.BinDirectory, "SonaTribe*.dll")));
  
  
          }
  
          /// &lt;summary&gt;
          /// See http://fishbowl.pastiche.org/2004/01/19/persistent_login_cookie_best_practice/
          /// &lt;/summary&gt;
          /// &lt;param name="sender"&gt;&lt;/param&gt;
          /// &lt;param name="e"&gt;&lt;/param&gt;
          protected void Session_Start(object sender, EventArgs e)
          {
              if (Container != null)
              {
                  var accountService = Container.Resolve&lt;IAccountService&gt;();
                  var logger = Container.Resolve&lt;ILogger&gt;();
                  var forms = Container.Resolve&lt;IFormsAuthentication&gt;();
  
                  // if there is a cookie
                  if (Context.Request.Cookies["user-id"] != null)
                  {
                      try
                      {
                          //get the new cookie key from the server
                          var newUserSessionResponse = accountService.RegisterNewUserSession(new RegisterNewUserSessionRequest
                          {
                              SessionId = Context.Request.Cookies["user-id"].Value
                          });
  
                          if (newUserSessionResponse.Success)
                          {
                              //do something
                          }
                          else
                          {
                              logger.Info("Failed attaching the user to the session", newUserSessionResponse.Message);
                          }
                      }
                      catch (Exception exc)
                      {
                          logger.Error("Failed attaching the user to the session", exc);
                      }
                  }
                  else
                  {
                      //new user:
                     //do things
                  } 
              }
          }
  </code></pre>
  
  <p>Thanks</p>
  
  <p>w://</p>