title: ASP.NET MVC: How to use HttpContext.User
body: <p>Im getting really lost on how to use HttpContext.User. I read everywhere that its great for FormAutherication, but i just cant see how it works. If i do something like this:</p>
  
  <pre><code>ControllerContext.HttpContext.User = new GenericPrincipal(GetUser(username, password), roles);
  </code></pre>
  
  <p>What does ControllerContext.HttpContext.User contain? and how do i access information about the user this way?</p>
  
  <p>Im think that i have a Action like this:</p>
  
  <pre><code>public User GetUser(string username, string password)
      {
          try
          {
              var user = (from u in dm.Users
                          join r in dm.Roles
                          on u.Role_ID_FK equals r.RoleID
                          where u.Username.Equals(username) &amp;&amp; u.Password.Equals(password)
                          select u).Single();
  
              return user;
          }
          catch (Exception e)
          {
              return null;
          }
      }
  </code></pre>
  
  <p>And then if i want user information in my view, like the user name or role, i can call ControllerContext.HttpContext.User.Username in my View. But this is diffenrently the wrong way to look at it.</p>
  
  <p>So can you guys give me a kick in the rigth direction or post a link to a site which can?</p>