Get user information from Auth0 using ASP.NET MVC 6 WebAPI

I use JwtBearerAuthentication in my WebAPI (ASP.NET Core RC1) to authenticate (Auth0) users who access my API. In Startup.cs, I configure the connection to Auth0 using the following code. What am I missing to access user information about each user accessing the API?

app.UseJwtBearerAuthentication(options => { options.AutomaticAuthenticate = true; options.AutomaticChallenge = true; options.Audience = clientId; options.Authority = domain; options.Events = new JwtBearerEvents { OnValidatedToken = context => { var claimsIdentity = context.AuthenticationTicket.Principal.Identity as ClaimsIdentity; claimsIdentity.AddClaim(new Claim("id_token", context.Request.Headers["Authorization"][0].Substring(context.AuthenticationTicket.AuthenticationScheme.Length + 1))); return Task.FromResult(0); } }; }); 
+5
source share
1 answer

First of all, apologies that the sample I am giving you is in RC2. I do not have RC1 on my computer and installing it after I installed RC2 is not a risk that I want to take. If for some reason you can’t switch to RC2, then I hope you can modify this sample to RC1.

OK, so it’s important to understand first that the information you can get about the user will be limited to what is contained in the JWT. Therefore, when requesting a token, be sure to set the correct scope. For example, if you want to specify the name and email address of a user, be sure to set the scope to openid name email .

Well, if you want to access the information inside the OnTokenValidated event, you can use the following code:

 var options = new JwtBearerOptions { Audience = Configuration["auth0:clientId"], Authority = $"https://{Configuration["auth0:domain"]}/", Events = new JwtBearerEvents { OnTokenValidated = context => { // If you need the user information for any reason at this point, you can get it by looking at the Claims property // of context.Ticket.Principal.Identity var claimsIdentity = context.Ticket.Principal.Identity as ClaimsIdentity; if (claimsIdentity != null) { // Get the user ID string userId = claimsIdentity.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value; // Get the name string name = claimsIdentity.Claims.FirstOrDefault(c => c.Type == "name")?.Value; } return Task.FromResult(0); } } }; app.UseJwtBearerAuthentication(options); 

If you want to access information from within a controller action, you can simply view the User formulas, for example.

 public class ValuesController : Controller { [Authorize] [HttpGet] [Route("userinfo")] public object UserInformation() { string userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value; // Get the name string name = User.Claims.FirstOrDefault(c => c.Type == "name")?.Value; return new { UserId = userId, Name = name }; } } 

If you need more information about the user, you can also use our full .NET SDK for the management API and use the methods associated with it to get additional information about the user. However, my suggestion would rather be to make sure that you set the correct scope when issuing the token and make sure they are contained in the JWT token.

A full sample is available at https://github.com/auth0-samples/auth0-aspnetcore-webapi-userinfo

+11
source

All Articles