title: Getting the logged in username in ASP.NET MVC3 intranet application
body: <p>I am working on a MVC 3 intranet application ( windows authentication ). The application must display the profile page of the user once a user logs in. In order to do that the username of the logged in user must be passed in as a route parameter in the following route in Global.asax.cs.</p>
  
  <pre><code>routes.MapRoute(
      "Initial",
      "{controller}/{action}/{emailAlias}", // URL with parameters
      new { controller = "Home", action = "Home", userId = **&lt;USERNAME&gt;**}
  );
  </code></pre>
  
  <p>Here, for the  I've used some alternatives.</p>
  
  <ol>
  <li><p>At first I used <code>Environment.Username</code>. Which works well in development. But not after publishing. Because then <code>Environment.Username</code> yields the name of the applications pool which the app runs in. As described <a href="http://msdn.microsoft.com/en-us/library/gg703322%28VS.98%29.aspx" rel="noreferrer">here</a>. </p></li>
  <li><p><code>Controller.User.Identity.Name</code> is used to get the desired username in controllers, works well in pre and post publishing. But it cannot be used in the context of Global.asax.cs. </p></li>
  <li><p><code>System.Web.HttpContext.Current.User.Identity.Name</code> yields null reference. </p></li>
  <li><p><code>System.Security.Principal.WindowsIdentity.GetCurrent().Name</code> works kind of same as <code>Environment.Username</code></p></li>
  </ol>
  
  <p>I'm sure it's easy to figure out that I'm a noob by now. Maybe I've missed something obvious. If so please point that out or please tell me of a simple solution thanks in advance.  </p>