Auth0 Home Blog Docs

How to update user metadata of current user in ASP.NET Core?


#1

Once a user has logged in I’m trying to let them add an Organisation properly against their username in Auth0. I have written this quick test method:

[Authorize]
    public async Task<IActionResult> UpdateProfile()
    {
        var userId = this.User.FindFirst(ClaimTypes.NameIdentifier).Value;
        
        var client = new ManagementApiClient(await GenerateManagementApiToken(), new Uri($"https://{_configuration["Authentication:Auth0:Domain"]}"));
        var user = await client.Users.GetAsync(userId);
        Auth0UserMetaData metadata = Auth0UserMetaData.FromJSON(user.UserMetadata);
        metadata.OrganisationId = "123456";

        var response = await client.Users.UpdateAsync(userId, new Auth0.ManagementApi.Models.UserUpdateRequest
        {
            UserMetadata = metadata.ToJSON()
        });

        return View();
    }

This returns an error that it cannot get the user. I don’t know what userid should be?


#3

:wave: @strich are you updating the metadata when a user logs in? You could use a Rule to do this (https://auth0.com/docs/rules/current/metadata-in-rules).

I’m not too familiar with the ASP.NET Core SDK, but for your UpdateProfile() method, what is returned in your userId variable? The userid is usually in the format (identity provider)|(unique id in the provider) and depending on the connection, it could look something like auth0|1234567890 or facebook|1234567890


#4

Hi @kim.noel thanks for responding. I managed to get this one solved last night - My key issue was that my audience string wasn’t correct in the end.


#5

Great! Glad you were able to fix it.