title: How to get User.Identity working in a class outside of a Controller
body: <p>After a user Logs in to the system, i need to put a variable in the Session. I am doing this with an extension method:</p>
  
  <p>Login method:</p>
  
  <pre><code>[HttpPost]
          public ActionResult LogOn(LogOnModel model, string returnUrl)
          {
              if (ModelState.IsValid)
              {
                  if (Membership.ValidateUser(model.UserName, model.Password))
                  {
                      FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
  
                      int id = this.HttpContext.GetFirmaId();
                      String username = this.HttpContext.User.Identity.Name;
  
                      //SessionExtensions.SetFirmaId(Session, model.UserName);
  
                      if (Url.IsLocalUrl(returnUrl) &amp;&amp; returnUrl.Length &gt; 1 &amp;&amp; returnUrl.StartsWith("/")
                          &amp;&amp; !returnUrl.StartsWith("//") &amp;&amp; !returnUrl.StartsWith("/\\"))
                      {
                          return Redirect(returnUrl);
                      }
                      else
                      {
                          if (User.IsInRole("Administrator"))
                          {
                              RedirectToAction("Index", "Admin");
                          }
                          else
                          {
                              return RedirectToAction("Index", "Home");
                          }
                      }
                  }
                  else
                  {
                      ModelState.AddModelError("", "Brugernavn eller password er forkert.");
                  }
              }
  
              // If we got this far, something failed, redisplay form
              return View(model);
          }
  </code></pre>
  
  <p>Extension method:</p>
  
  <pre><code>public static int GetFirmaId(this HttpContextBase context)
      {
  
          var firmaid = context.Session["firmaid"] as int?;
  
          if (firmaid == null)
          {
              // Hvis firmaid er røget ud af Session, skal den firmaid som tilhører brugeren hentes
  
              firmaid = SetFirmaId(context.Session, context.User.Identity.Name);
  
          }
  
          return (int)firmaid;
      }
  </code></pre>
  
  <p>The problem is that <code>context.User.Identity.Name</code> returns an empty string.</p>
  
  <p>How can i solve this problem?</p>