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 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;
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
source share