title: Using formsauthentication for login and use HttpContext.Current.User.Identity
body: <p>I have made a website with a page that includes a razor form. The user can login on this form and then redirects to a different page.
  The logging in (and logging out) works with formsauthentication succesfully. However, I can't seem to use HttpContext.Current.User.Identity.Name to retrieve the stored username (in the formsauthentication cookie). It returns an empty string "". </p>
  
  <p>I am using MVC 5 and ASP 4.5 with no standard membership or role providers.</p>
  
  <p>Login:</p>
  
  <pre><code> [HttpPost]
          public ActionResult Login(User user)
          {
              if (ModelState.IsValid)
              {
                  bool authenticated = userscontroller.isAuthorized(user.Email, user.Password);
                  if (authenticated)
                  {
                      if (userscontroller.isAuthenticated())
                      {
                          userscontroller.deAuthenticateUser();
                      }
                      userscontroller.authenticateUser(user);
                      return Redirect(Url.Action("Index", "Home"));
                  }
              }
          }
  </code></pre>
  
  <p>Authenticating the user:</p>
  
  <pre><code> public void authenticateUser(User user)
      {
          FormsAuthentication.SetAuthCookie(user.Username, false);
      }
  </code></pre>
  
  <p>Then getting the name of the user:</p>
  
  <pre><code>public User userFromCookie()
  {
      if (isAuthenticated())
      {
          return getUserByUsername(HttpContext.Current.User.Identity.Name);
      }
      else { return null; }
  }
  </code></pre>
  
  <p>isauthenticated()</p>
  
  <pre><code>public bool isAuthenticated()
  {
      if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
      {
          return true;
      }
      else
      {
          return false;
      }
  }
  </code></pre>
  
  <p>Webconfig:</p>
  
  <pre><code>&lt;authentication mode="Forms"&gt;
    &lt;forms loginUrl="~/Account/Login" timeout="2880" /&gt;
  &lt;/authentication&gt;
   &lt;authorization &gt; &lt;deny users="?"/&gt; &lt;/authorization&gt;
  </code></pre>
  
  <p>So the identity.name returns "". </p>
  
  <p>Help is appreciated!</p>