Retrieve user names from Auth0 and display them in a Razor file

I’m working in the .NET Core 3.1 MVC framework. I managed to make it so that users can access certain parts of my app only after they have logged in with Auth0. Great, now I’d like to retrieve all the user names from Auth0 and display them in a Razor file in my app. Is this possible? From what I’ve searched, I suspect it has to do with the Auth0 Management API where I use Rest requests, but I’m clueless when it comes to actually implementing that in the .NET Core MVC framework. I’m a beginner, any help is much appreciated!

Hey there!

That is correct in order to retrieve user names from Auth0 you can utilise Management API. Here’s the endpoint for that:

https://auth0.com/docs/api/management/v2#!/Users/get_users_by_id

and here’s how to make a GET request in .NET:

Sorry, not quite what I was looking for. The example from StackOverflow uses a different API from Auth0 (StackExchange). Maybe it’s obvious to an advanced user but I’m a total noob. I understand I have to make use of the Get User Endpoint to achieve this. Here is an example request to the endpoint from your site (link):

var client = new RestClient("https://YOUR_DOMAIN/api/v2/users?q=email%3A%22jane%40exampleco.com%22&search_engine=v3");
var request = new RestRequest(Method.GET);
request.AddHeader("authorization", "Bearer YOUR_MGMT_API_ACCESS_TOKEN");
IRestResponse response = client.Execute(request);

Provided I know my domain and I’ve generated the access token (both of which I have), where would I even put this code in my .NET Core MVC app?

Here is how you do it. Create a model called UserDetails.cs and fill it with the following code:

using System;
using System.Collections.Generic;

namespace YourAppName.Models
{
    public class UserDetails
    {
        public DateTime created_at { get; set; }
        public string email { get; set; }
        public bool email_verified { get; set; }
        public string family_name { get; set; }
        public string given_name { get; set; }
        public List<Identity> identities { get; set; }
        public string locale { get; set; }
        public string name { get; set; }
        public string nickname { get; set; }
        public string picture { get; set; }
        public DateTime updated_at { get; set; }
        public string user_id { get; set; }
        public DateTime last_login { get; set; }
        public string last_ip { get; set; }
        public int logins_count { get; set; }
        public AppMetadata app_metadata { get; set; }

        public List<UserDetails> userList { get; set; }

        public UserDetails()
        {
            userList = new List<UserDetails>();
        }
    }

    public class Identity
    {
        public string provider { get; set; }
        public string access_token { get; set; }
        public int expires_in { get; set; }
        public string user_id { get; set; }
        public string connection { get; set; }
        public bool isSocial { get; set; }
    }

    public class AppMetadata
    {
        public List<string> roles { get; set; }
    }
}

In your controller file, create the following action method:

public ActionResult Index()
        {
            string accessToken = "YOUR_AUTH0_MANAGEMENT_TOKEN_GOES_HERE";
            using (var client = new HttpClient())
            {
                if (!string.IsNullOrEmpty(accessToken))
                {
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                }

                HttpResponseMessage result = client.GetAsync("YOUR_AUTH0_IDENTIFIER_URL_GOES_HERE").Result;

                string apiResponse = result.Content.ReadAsStringAsync().Result;

                UserDetails users = new UserDetails();
                users.userList = JsonConvert.DeserializeObject<List<UserDetails>>(apiResponse);
                return View(users);
            }
        }

Finally, in your Razor file, do this:

@model YourAppName.Models.UserDetails

@foreach (var item in Model.userList)
                    {
                        <p>@Html.DisplayFor(modelItem => item.name)</p>
                    }

This will list all the user names from your Auth0 account.

Also, note that the validation token will expire every 24 hours, so don’t be surprised when your app works one day, and the next, it throws an error. Just change the token for a new one, and you are good to go for another 24hrs.

1 Like

Perfect! Glad that you have figured it out!

1 Like