title: Displaying User Information after Authentication in ASP.NET Core
body: <p>I have already implemented Identity with JWT token and secured the routes using <code>[Authorize]</code> attribute. This works well. There are several tutorials on securing a route using JWT and Identity but I couldn't find any on how to let a particular user only access their own stuff after they have been authenticated.</p>
  <p>So far, I have done this:</p>
  <pre class="lang-cs prettyprint-override"><code>[Authorize]
  [HttpGet(&quot;protected&quot;)]
  public async Task&lt;string&gt; ProtectedArea()
  {
      var userName = User.Identity.Name;
      var user = repository.GetUserByEmail(userName);
      //var user2 = _userManager.FindByIdAsync(userName); //user2.id gives null reference error
      return User.Bio;
  }
  </code></pre>
  <p>Here, <code>GetUserByEmail</code> is a simple method that returns the User object by querying the database(i couldn't get the <code>UserManager</code> to work here either to return the User using <code>FindByEmailAsync</code> as it would throw a null reference exception)</p>
  <p>Here's what my <code>claims</code> looks like:</p>
  <pre class="lang-cs prettyprint-override"><code>var claims = new List&lt;Claim&gt;
  {
      new Claim(JwtRegisteredClaimNames.Sub, email),
      new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
      new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
      new Claim(JwtRegisteredClaimNames.UniqueName, user.UserName),
      new Claim(JwtRegisteredClaimNames.NameId, user.Id.ToString())
  };
  </code></pre>
  <p>So what is the standard way for identifying a user from the token and then go on to display that user's info? What I have done above seems a little hacky to me but is it the right approach? Also, do I need to perform a check on every route and get the User Id via a method or is it possible to do so via some attribute as well?</p>
  <p>Any help would be greatly appreciated.</p>