Auth0 Home Blog Docs

How to identift a user in my .net Core Api?

angular
net-core
identity
#1

I’ve been following these two tutorials https://auth0.com/docs/quickstart/backend/aspnet-core-webapi 1 & https://auth0.com/docs/quickstart/spa/angular2 1 in order to make my Angular-SPA call my .net Core Api.

The authentication part works fine but I would also like to identify the user that is logging in. How do I do this?

I’ve tried adding this Custom Claim using a rule in order to add the email to the Access Token, however, I cannot see anything in my Api when it is called.

function (user, context, callback) {
context.accessToken[“https://something/api/v2/email”] = user.email;
callback(null, user, context);
}

Here is the .net Core Api endpoint that I’m calling.

[HttpGet("{profileId}")]
[Authorize]
public Task Get(string profileId)
{
var authorization = this.Request.Headers[“Authorization”].ToString();
var tokenstring = authorization.Substring("Bearer ".Length).Trim();
var handler = new JwtSecurityTokenHandler();
var token = handler.ReadJwtToken(tokenstring);

var principal = HttpContext.User;
  if (principal?.Claims != null)
  {
     foreach (var claim in principal.Claims)
     {
           Console.WriteLine($"CLAIM TYPE: {claim.Type}; CLAIM VALUE: {claim.Value}");
     }
  }

return GetProfileByIdInternal(profileId);
}

As you can see from this screenshot there is no sign of any custom claims

The Startup.cs file and everything in the Agular project are exactly as described in the two tutorials mentioned earlier.

#2

jmangelo helped me solve this problem in another topic - Cannot see userinfo in Api endpoint

1 Like
closed #3

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.