How to get equivalent of “User name” using Microsoft Graph

拈花ヽ惹草 提交于 2020-08-02 04:46:27

问题


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

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.


回答1:


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;
 }



回答2:


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 #.




回答3:


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.



来源:https://stackoverflow.com/questions/51903113/how-to-get-equivalent-of-user-name-using-microsoft-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!