I have a .Net Core 2 Web API project that is using JwtBearer Authentication. However, while this works in regards to Authenticating my user and respecting the [Authorize]
attribute on my controller, the User Identity and Claims are never populated. Is there something more I have to do to create ensure that a ClaimsIdentity is created for my authenticated user?
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
...
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.Authority = "https://myTenant.auth0.com";
options.Audience = "https://localhost:5001/api/v1";
options.RequireHttpsMetadata = false;
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = "myTenant.auth0.com",
ValidAudience = "https://localhost:5001/api/v1"
};
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, SystemModelBuilder modelBuilder)
{
...
app.UseHttpsRedirection();
app.UseAuthentication();
}
My token looks like this on jwt.ms:
{
"typ": "JWT",
"alg": "RS256",
"kid": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
}
.{
"profileData": {
"given_name": "Rodd",
"family_name": "Harris"
},
"iss": "https://myTenant.auth0.com/",
"sub": "auth0|37b5f5c54fdac",
"aud": [
"https://localhost:5050/api/v1/",
"https://myTenant.auth0.com/userinfo"
],
"iat": 1532628090,
"exp": 1532629290,
"azp": "2RdsNtKpUgh_wgB3NI6gxd-OAl",
"scope": "openid profile email app.client.read"
}.[Signature]
In my controller, I have something like this:
[ApiController]
[Route("api/v1/[controller]")]
[Authorize]
public class TestController : ControllerBase
{
[HttpGet("clients")]
public async Task<IActionResult> ClientsList()
{
//Debugger gets here -- user is authenticated
var user = HttpContext.User.Identity.Name; //Always null
var count = HttpContext.User.Claims.Count(); //Always 0
var allowed = HttpContext.User.IsAuthenticated; //Always true
var type = HttpContext.User.AuthenticationType; //AuthenticationTypes.Federated
...
}
What keeps the user's name and claims from being created?
Update
So I just realized I AM actually getting the claims mapped into HttpContext.User.Identity.Claims
. Also realized that my token doesn't have a name value to automagically map to the Identity. So, I guess my real question is, how do I override the custom mapping and do my own mapping of the token into an Identity?