I am trying to make a very simple JWT bearer authentication implementation using ASP.NET Core. I am returning a response from the controller like this:
var identity = new ClaimsIdentity(); identity.AddClaim(new Claim(ClaimTypes.Name, applicationUser.UserName)); var jwt = new JwtSecurityToken( _jwtOptions.Issuer, _jwtOptions.Audience, identity.Claims, _jwtOptions.NotBefore, _jwtOptions.Expiration, _jwtOptions.SigningCredentials); var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt); return new JObject( new JProperty("access_token", encodedJwt), new JProperty("token_type", "bearer"), new JProperty("expires_in", (int)_jwtOptions.ValidFor.TotalSeconds), new JProperty(".issued", DateTimeOffset.UtcNow.ToString()) );
I have Jwt middleware for incoming requests:
app.UseJwtBearerAuthentication(new JwtBearerOptions { AutomaticAuthenticate = true, AutomaticChallenge = true, TokenValidationParameters = tokenValidationParameters });
This seems to work to protect resources with the authorize attribute, but claims never appear.
[Authorize] public async Task<IActionResult> Get() { var user = ClaimsPrincipal.Current.Claims;
source share