Home > Blockchain >  Get User Name by its ID Azure AD
Get User Name by its ID Azure AD

Time:10-20

We use Azure AD for authentication and store users' IDs (object IDs) in our API. We need to resolve these IDs into user names. I tried to do it using Microsoft graph and On-Behalf-Of flow to exchange our token to another with the required scope but it is quite complex and requires User.Read.All permissions for users.

So my question is - Is any other approach how to resolving the user Id into a name?

CodePudding user response:

get any user's name by it's ID ---> then you must give application enter image description here

And then you have to use client credential flow to get authorization and call graph api. I'm afraid On-Behalf-Of is not suitable for you. Try my code below.

using Microsoft.Graph;
using Azure.Identity;

var scopes = new[] { "https://graph.microsoft.com/.default" };
string tenantId = "TenantId";
string clientId = "ClientId";
string clientSecret = "ClientSecret";
var clientSecretCredential = new ClientSecretCredential(
                tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var user = await graphClient.Users["user_id"].Request().GetAsync();
  • Related