Last time around we added Auth0 to a project to handle our login and signup. You can see that post here

With Auth0 handling our login and signup we want to connect to Auth0 and get the user information.

Source code for this can be found here

Auth0 Management API

This is were Auth0 management API comes into play. We use this to connect our app to Auth0 and get the user information.

Getting tokens

To access the management API from our code we need to create a machine to machine connection.

See here for more information on generating a token to use.

Time for some code

First up we want to add the Nuget package for the Auth0 Management API

Install-Package Auth0.ManagementApi 

Auth0 Get user information

Now we can get into the good stuff. To get the user information from the Auth0 we need to call this

var client = new ManagementApiClient("TOKEN", new Uri("https://DOMAIN.eu.auth0.com/api/v2/"));

return client.Users.GetAsync(USERID).Result;

That's pretty much it. Those 2 lines of code. We take their user ID from our identity management and then send it to Auth0 and it will give us back the users information. A sample of the fields available to use in this API.

{
  "email": "[email protected]",
  "email_verified": false,
  "username": "johndoe",
  "phone_number": "+199999999999999",
  "phone_verified": false,
  "user_id": "usr_5457edea1b8f33391a000004",
  "created_at": "",
  "updated_at": "",
  "identities": [
    {
      "connection": "Initial-Connection",
      "user_id": "5457edea1b8f22891a000004",
      "provider": "auth0",
      "isSocial": false
    }
  ],
  "app_metadata": {},
  "user_metadata": {},
  "picture": "",
  "name": "",
  "nickname": "",
  "multifactor": [
    ""
  ],
  "last_ip": "",
  "last_login": "",
  "logins_count": 0,
  "blocked": false,
  "given_name": "",
  "family_name": ""
}

In my code I added a new method to my HomeController


    private async Task<Auth0.ManagementApi.Models.User> GetUserInformationAsync(string sUserId)
    {
      var client = new ManagementApiClient("YOURTOKEN", new Uri("https://YOURDOMAIN.eu.auth0.com/api/v2/"));

      return client.Users.GetAsync(sUserId).Result;
    }

I then check if my user is authenticated.


      // Get User information from Auth0
      if (User.Identity.IsAuthenticated)
      {
        string sUserId = User.Claims.Last()?.Value;


        Task<Auth0.ManagementApi.Models.User> _taskUserInfo = GetUserInformationAsync(sUserId);
      }

If the user is Authenticated I get the User ID and then call my new method to get the user information.

Summary

That's it I used Auth0 to handle the login and signup and then I called the Auth0 API to get user information like email and name.

I have done this live in a few places in production for example touroperator.io I have been using these posts to work out how to make the signup work better.

On Touroperator.io when you log in or signup I check if I have a record of in a tenant table. If not I then call the management API to get the user information. Then I create a tenant on my side. It really makes it easier to build out logins.