title: How to get user information after sending JWT to a new controller
body: <p>I'm working on an ASP.Net Core API project.</p>
  
  <p>There are 2 controllers, controller A and B, I use Postman to pass my username and password to controller A action method, and it successfully returns a JWT.
  After I pass the JWT to controller B, I hope to get the username and role information of the credential which I had input into the controller A.</p>
  
  <p>The code in controller B below shows the way I get the user info.</p>
  
  <pre><code>public async Task&lt;ActionResult&gt; Get()
          {
              var user = await _userManager.GetUserAsync(HttpContext.User);
              var role = await _userManager.GetRolesAsync(user);
              return Ok(user);
          }
  </code></pre>
  
  <p>But after I sent the JWT, I got null user.</p>
  
  <p><a href="https://i.stack.imgur.com/YZNeo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YZNeo.png" alt="enter image description here"></a></p>
  
  <p>Edit1:</p>
  
  <p>ConfigureServices method in my Startup.cs:</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
          {
              services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
  
  services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                  .AddJwtBearer(options =&gt;
                  {
                      options.TokenValidationParameters = new TokenValidationParameters
                      {
                          ValidateIssuer = true,
                          ValidateAudience = true,
                          ValidateIssuerSigningKey = true,
                          ValidIssuer = "issuer",
                          ValidAudience = "audience",
                          IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("This is my super long security Key 123"))
                      };
                  });
  
              services.AddDbContext&lt;JWT_AuthContext&gt;(option =&gt; option.UseSqlServer(Configuration.GetConnectionString("JWT_AuthContextConnection")));
  
          }
  </code></pre>
  
  <p>IdentityHostingStartup.cs:</p>
  
  <pre><code>public class IdentityHostingStartup : IHostingStartup
      {
          public void Configure(IWebHostBuilder builder)
          {
              builder.ConfigureServices((context, services) =&gt; {
                  services.AddDbContext&lt;JWT_AuthContext&gt;(options =&gt;
                      options.UseSqlServer(
                          context.Configuration.GetConnectionString("JWT_AuthContextConnection")));
  
                  services.AddDefaultIdentity&lt;JWT_AuthUser&gt;()
                      .AddRoles&lt;JWT_AuthRole&gt;()
                      .AddEntityFrameworkStores&lt;JWT_AuthContext&gt;();
              });
          }
      }
  </code></pre>
  
  <p>Controller Action for JWT generation (Controller A in the previous context):</p>
  
  <pre><code>[Route("api/[controller]")]
      [ApiController]
      public class AuthController : ControllerBase
      {
          private UserManager&lt;JWT_AuthUser&gt; _userManager;
          public AuthController(UserManager&lt;JWT_AuthUser&gt; userManager)
          {
              _userManager = userManager;
          }
  
          [HttpPost("token")]
          public async Task&lt;ActionResult&gt; GetToken()
          {
              var header = Request.Headers["Authorization"];
              if(header.ToString().StartsWith("Basic"))
              {
                  var loginInfo = header.ToString().Substring("Basic ".Length).Trim();
                  var userNameAndPassword = Encoding.UTF8.GetString(Convert.FromBase64String(loginInfo)); //username:password
                  var userName = userNameAndPassword.Split(":")[0];
                  var password = userNameAndPassword.Split(":")[1];
                  var user = await _userManager.FindByNameAsync(userName);
  
                  if (user != null &amp;&amp; await _userManager.CheckPasswordAsync(user,password))
                  {
                      //security key
                      string securityKey = "This is my super long security Key 123";
  
                      //symmetric security key
                      var symmetricSecurityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(securityKey));
  
                      //signing credentials
                      var signingCredentials = new SigningCredentials(symmetricSecurityKey, SecurityAlgorithms.HmacSha256Signature);
  
                      //add claims
                      var claims = new List&lt;Claim&gt;();
  
                      //create token
                      var token = new JwtSecurityToken(
                          issuer: "issuer",
                          audience: "audience",
                          expires: DateTime.Now.AddHours(1),
                          claims: claims,
                          signingCredentials: signingCredentials
                          );
  
                      //return token
                      return Ok(new JwtSecurityTokenHandler().WriteToken(token));
                  }
  
              }
  
              return Unauthorized();
  }
  </code></pre>
  
  <p>Controller method for getting user info (Controller B in the previous context):</p>
  
  <pre><code>[Authorize]
      [Route("api/[controller]")]
      [ApiController]
      public class ValuesController : ControllerBase
      {
  
          private UserManager&lt;JWT_AuthUser&gt; _userManager;
          public ValuesController(UserManager&lt;JWT_AuthUser&gt; userManager)
          {
              _userManager = userManager;
          }
  
          // GET api/values
          [HttpGet]
          public async Task&lt;ActionResult&gt; Get()
          {
              var user = await _userManager.GetUserAsync(HttpContext.User);
              var role = await _userManager.GetRolesAsync(user);
              return Ok(user);
          }
  }
  </code></pre>
  
  <p>Edit 2:
  Startup.cs Configure</p>
  
  <pre><code>public void Configure(IApplicationBuilder app, IHostingEnvironment env)
          {
              if (env.IsDevelopment())
              {
                  app.UseDeveloperExceptionPage();
              }
              else
              {
                  // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                  app.UseHsts();
              }
  
              app.UseHttpsRedirection();
              app.UseAuthentication();
              app.UseMvc();
          }
  </code></pre>