Auth0 Home Blog Docs

ManagementApiClient missing FirstName LastName

firstname-and-lastna

#1

I have a set of users from Username-Password-Authentication connection setup.
These users do not come with given_name and family_name so I added them to user_metadata information like
user_metadata: { "given_name": "FirstName", "family_name": "LastName", }

and I made a new Rule to add them to the user context like
if (user.user_metadata.given_name) {
user.given_name = user.user_metadata.given_name; }
if (user.user_metadata.family_name) {
user.family_name = user.user_metadata.family_name; }

The resulting user context looks fine and it has given_name and family_name the same way that other users have.

The problem is that when I use Management API in C# the FirstName and LastName are empty strings for these users.

var client = new ManagementApiClient(tokenResponse.AccessToken, new Uri(_auth0Options.Value.ApiIdentifier)); var users = await client.Users.GetAllAsync(); var result= users.Select(p => new { p.FirstName, p.LastName, })


#2

hi @arazmjou I’m trying to understand your question a bit more… is it C# just putting empty strings there or just not able to retrieve the fields (family_name for example) from user_metadata? I think it’s the latter but trying to understand what’s your issue since I cannot see it from that code snippet. Thanks in advance.


#3

The quick fix for that would be to use UserMetadata directly

var users = await client.Users.GetAllAsync();  
            var enumerable = users
                .Select(p => new
                {
                    FirstName = string.IsNullOrWhiteSpace(p.FirstName) ? p.UserMetadata?.given_name : p.FirstName,
                    LastName = string.IsNullOrWhiteSpace(p.LastName) ? p.UserMetadata?.family_name : p.LastName});

But I wish I could get the FirstName and the LastName directly from the API like:

 var client = new ManagementApiClient(tokenResponse.AccessToken, 
           new Uri(_auth0Options.Value.ApiIdentifier));      
var users = await client.Users.GetAllAsync(); 
var result= users.Select(p => new {p.FirstName, p.LastName })

As I already have a rule

function (user, context, callback) {  
  if (user.user_metadata.given_name) 
    user.given_name = user.user_metadata.given_name;
  
  if (user.user_metadata.family_name) 
    user.family_name = user.user_metadata.family_name;   
  callback(null, user, context);
}