title: How can I use User.Identity.Name to work in a class outside of a Controller?
body: <p>How can I use <code>User.Identity.Name</code> to work in a class outside of a Controller?
  I just used like this inside class</p>
  
  <pre><code>private readonly static string sLogon =  HttpContext.Current.User.Identity.Name.Substring(HttpContext.Current.User.Identity.Name.LastIndexOf(@"\") + 1);
  </code></pre>
  
  <p>It retruns null reference on IIS 7.5 production server even It works on development server.</p>
  
  <p>Here, I've used some alternatives.</p>
  
  <ul>
  <li>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="nofollow">here</a>. </li>
  <li><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 Class .cs. </li>
  <li><code>System.Web.HttpContext.Current.User.Identity.Name</code> yields null reference. </li>
  <li><code>System.Security.Principal.WindowsIdentity.GetCurrent().Name</code> works kind of same as <code>Environment.Username</code></li>
  </ul>
  
  <p>Do you have any ideas how to use it? Thanks</p>
  
  <p><strong>Update</strong></p>
  
  <p>According to environment, If I get the login id, I can get employee information such as EmployeeID, Email, etc... from Employee table. for e.g : I have one class (outside of a controller) to get Employee ID from Employee table to access like this.</p>
  
  <p><code>string EmployeeID = new EmployeeService().EmployeeID();</code></p>
  
  <pre><code>private readonly static string sLogon =  HttpContext.Current.User.Identity.Name.Substring(HttpContext.Current.User.Identity.Name.LastIndexOf(@"\") + 1);
  
  public string EmployeeID()
          {
              var employee = new Common().Employee();
  
              string sID = (from e in employee
                            where (e.LogonID ?? "N/A").ToLower().Contains(sLogon.ToLower())
                            select e.EmployeeID).FirstOrDefault();
  
              return sID;
          }
  </code></pre>