title: Get Username of Currently interacting User in WCF both Rest Endpoint and MVC3 with Windows Authentication
body: <p>In an MVC3 project that I'm working on we're trying to move a lot of our logic that is currently in the controllers into a service layer and expose it as a REST Service in WCF.</p>
  
  <p>So in our Global.asax we create a Service Route like so:</p>
  
  <pre><code>RouteTable.Routes.Add(new ServiceRoute
      ("Exampleservice", new WebServiceHostFactory(), typeof(ExampleService)));
  </code></pre>
  
  <p>and our controllers access the service something like this:</p>
  
  <pre><code>public class ExampleController : Controller {
      private IExampleService service;
  
      public ExampleController() {
          this.service = new ExampleService();
      } 
  
      public ActionResult Index() {
           var results = service.GetAll();
           return View(results);
      }
  }
  </code></pre>
  
  <p>The main point here being that we use the service class directly (without making requests over the network with an HttpClient).</p>
  
  <p>Our website uses Windows Authentication (it's an intranet site) and we would like to keep it that way. My Question is, is there a way that I can get the User's Identity in the service class that will work both for how we have the Controllers using the service, and the way that WCF uses the service?</p>
  
  <p>For example:</p>
  
  <pre><code>[ServiceContract]
  public interface IExampleService
  {
      [WebGet(UriTemplate = "/")]
      List&lt;Results&gt; GetAll();
  }
  
  public class ExampleService : IExampleService
  {
       List&lt;Results&gt; GetAll() {
           // Get User Name Here
           // In ASP.Net I would use User.Identity.Name
           // If I was just worrying about the the REST service I would use
           // ServiceSecurityContext.Current.WindowsIdentity.Name
       }
  }
  </code></pre>