title: Obtaining current user id from outside of controller
body: <p>I am currently implementing a Background Service in a web application, in which this services got an implementation of a method which do requires to get current logged in user id.</p>
  <p>i am trying to get access to the current logged in user, but making this call obviously from outside of a controller cause an error with this following
  Message = &quot;Object reference not set to an instance of an object.&quot;</p>
  <pre><code> public class ScopedProcessingService: IScopedProcessingService
  {
      private readonly ILogger&lt;ScopedProcessingService&gt; logger;
      private readonly IEweUserManager eweUserManager;
      private IHttpContextAccessor _contextAccessor;
      public ScopedProcessingService(IEweUserManager eweUserManager,
          ILogger&lt;ScopedProcessingService&gt; logger, IHttpContextAccessor contextAccessor)
      {
          this.logger = logger;
          this.eweUserManager = eweUserManager;
          _contextAccessor = contextAccessor;
      }
  
      public async Task DoWork(CancellationToken stoppingToken)
      {
          while (!stoppingToken.IsCancellationRequested)
          {
              try
              {
                  await Task.Delay(TimeSpan.FromSeconds(10), stoppingToken);
        var userId = _contextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);
    /* i am getting the error in this line */
                  
  
                  if (!string.IsNullOrEmpty(userId))
                  {
                      var result = await eweUserManager.FindAsync(userId);
                      if (result.IsSuccess)
                      {
                         .. the rest of the code
                      }
                      
                  }
              }
              catch(Exception ex)
              {
                  logger.LogError(ex, ex.Message);
              }
          }
      }
  }
  </code></pre>