Jim O'Neil
Jim O'Neil

Reputation: 23774

How to get equivalent of "User name" using Microsoft Graph

I want to get the same value from Microsoft Graph that the Azure Portal displays as the user name (as shown below):

enter image description here

userPrincipalName is close, but for guest users it has the #EXT and underscore encoding (e.g., jim.oneil_outlook.com#EXT#@redacted.onmicrosoft.com).

mail is NOT populated for member users, so I can't rely on that, although it seems to be what I want for guests.

To make matters worse, we do have a member user that also has a EXT address (where mail is not populated and userPrincipalName is encoded), so simply using one or the other property based on member type isn't correct either. Decoding and parsing the userPrincipalName seems brittle and kludgy to me.

Upvotes: 4

Views: 5190

Answers (3)

Louis Simonetti III
Louis Simonetti III

Reputation: 156

In your sample you are referring to the B2B users in Azure AD. B2B users are different in Azure AD, as you have noted in your question. In Azure AD, a B2B user, has a userType property value set to "Guest" - whereas local account to the tenant are set to "Managed". So you could have some logic that checks for the userType, for example in the graph, to view all those B2B users one could do a GET to the following endpoint https://graph.microsoft.com/v1.0/users?$filter=userType eq 'Guest'. Then you could also do an if statement something like

var logonName = "";  # holder for the logon name of the user
if(user.userType === 'Guest'){
  logonName = user.mail
} 
else {
  logonName = user.userPrincipalName
}

In short, the B2B users, you would want the user.mail attribute for that information.

Upvotes: 0

Keen Jin
Keen Jin

Reputation: 1138

According to your description, You want retrieve username. However, Microsoft Graph only provides MailNickName, UserPrincipalName, and mail.

Based on my test, we can only get the user name through UserPrincipalName for this case.

Although some users have EXT in their email address, they do not contain #. So we can exclude those users that exist by parsing userPrincipalName whether or not to include #.

Upvotes: 0

Tom Sun
Tom Sun

Reputation: 24569

I want to get the same value from Microsoft Graph that the Azure Portal displays as the user name.

According to user object, there is no equivalent of "user name" using Microsoft Graph Microsoft Graph.

Decoding and parsing the userPrincipalName seems brittle and kludgy to me.

I don't why decoding and parsing the userPrincipalName seems brittle and kludgy to you. Indeed it is a way to get the "User name". And we could do that easily.

If C# code is possible, you could refer to the following code.

if (userPrincipalName.Contains("#EXT#"))
 {
      userName = user.UserPrincipalName.Substring(0, userPrincipalName.IndexOf("#")).Replace('_', '@');
 }
 else
 {
    userName = user.UserPrincipalName;
 }

The following is the whole demo code

string graphResourceId = "https://graph.microsoft.com/";
string authority = "https://login.microsoftonline.com/{0}";
string tenantId = "tenant Id";
string clientId = "clientId";
string secret = "scret key";
authority = String.Format(authority, tenantId);
var graphserviceClient = new GraphServiceClient(
            new DelegateAuthenticationProvider(
                requestMessage =>
                {
                    requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);

                    return Task.FromResult(0);
                }));

 var displayName = "xxxx";
 var users = graphserviceClient.Users.Request().Select(x =>new 
 {
     x.UserPrincipalName,
     x.DisplayName

 } ).Filter("DisplayName eq '" + displayName + "'").GetAsync().Result;
 var user = users.FirstOrDefault();
 string userName = string.Empty;
 string userPrincipalName = user.UserPrincipalName;
 if (userPrincipalName.Contains("#EXT#"))
 {
      userName = user.UserPrincipalName.Substring(0, userPrincipalName.IndexOf("#")).Replace('_', '@');
 }
 else
 {
    userName = user.UserPrincipalName;
 }

Upvotes: 1

Related Questions