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

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?

:wave: @strich are you updating the metadata when a user logs in? You could use a Rule to do this (Manage Metadata with 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

Hi @kimcodes 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.

1 Like

Great! Glad you were able to fix it.