title: Correct way to use HttpContext.Current.User with async await
body: <p>I am working with async actions and use the HttpContext.Current.User like this</p>
  <pre><code>public class UserService : IUserService
  {
     public ILocPrincipal Current
     {
         get { return HttpContext.Current.User as ILocPrincipal; }
     }
  }
      
  public class ChannelService : IDisposable
  {
      // In the service layer 
      public ChannelService()
            : this(new Entities.LocDbContext(), new UserService())
        {
        }
  
      public ChannelService(Entities.LocDbContext locDbContext, IUserService userService)
      {
        this.LocDbContext = locDbContext;
        this.UserService = userService;
      }
  
      public async Task&lt;ViewModels.DisplayChannel&gt; FindOrDefaultAsync(long id)
      {
       var currentMemberId = this.UserService.Current.Id;
       // do some async EF request …
      }
  }
  
  // In the controller
  [Authorize]
  [RoutePrefix(&quot;channel&quot;)]
  public class ChannelController : BaseController
  {
      public ChannelController()
          : this(new ChannelService()
      {
      }
  
      public ChannelController(ChannelService channelService)
      {
          this.ChannelService = channelService;
      }
      
      // …
  
      [HttpGet, Route(&quot;~/api/channels/{id}/messages&quot;)]
      public async Task&lt;ActionResult&gt; GetMessages(long id)
      {
          var channel = await this.ChannelService
              .FindOrDefaultAsync(id);
   
          return PartialView(&quot;_Messages&quot;, channel);
      }
  
      // …
  }
  </code></pre>
  <p>I have the code recently refactored, previously I had to give the user on each call to the service.
  Now I read this article <a href="https://www.trycatchfail.com/2014/04/25/using-httpcontext-safely-after-async-in-asp-net-mvc-applications/" rel="noreferrer">https://www.trycatchfail.com/2014/04/25/using-httpcontext-safely-after-async-in-asp-net-mvc-applications/</a> and I’m not sure if my code still works.
  Has anyone a better approach to handle this? I don’t want to give the user on every request to the service.</p>