title: Getting current logged on user in web api controller
body: <p>I am trying to retrieve the current logged on user details in a Web API controller to create a user profile page. </p>
  
  <p>I have got the method created but the user keeps returning null in the code was shown below. This works fine in my MVC controller but in Web API controller it throws an error.</p>
  
  <pre><code>[HttpGet]
  public IActionResult userProfile()
  {
      if (ModelState.IsValid)
      {
          var user = _userManager.Users.First(x =&gt; x.Email == User.Identity.Name);
          return Ok(new UserViewModel
          {
              Id = user.Id,
              UserName = user.Email,
              FirstName = user.FirstName,
              LastName = user.LastName
          });
      }
      else
      {
          return BadRequest(ModelState);
      }
  }
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <pre><code>[HttpGet]
  [Authorize(Roles = "Client, Administrator")]
  public IActionResult userProfile()
  {
  
      string baseUrl = "https://localhost:5001";
      HttpClient client = new HttpClient();
      client.BaseAddress = new Uri(baseUrl);
      var contentType = new MediaTypeWithQualityHeaderValue("application/json");
      client.DefaultRequestHeaders.Accept.Add(contentType);
      client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", HttpContext.Session.GetString("token"));
  
      UserViewModel userModel = new UserViewModel();
      HttpResponseMessage response = client.GetAsync("https://localhost:5001/api/UserAPI").Result;
      string stringData = response.Content.ReadAsStringAsync().Result;
      userModel = JsonConvert.DeserializeObject&lt;UserViewModel&gt;(stringData);
  
      return View(userModel);
  }
  </code></pre>
  
  <p>Error Message: </p>
  
  <blockquote>
    <p>System.InvalidOperationException: 'Sequence contains no elements</p>
  </blockquote>