如何获得等效的“用户名"使用Microsoft Graph [英] How to get equivalent of "User name" using Microsoft Graph

查看:71
本文介绍了如何获得等效的“用户名"使用Microsoft Graph的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Azure门户显示为用户名的Microsoft Graph中获得相同的值(如下所示):

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

userPrincipalName已关闭,但对于来宾用户,它具有#EXT和下划线编码(例如,jim.oneil_outlook.com#EXT#@ redacted.onmicrosoft.com).

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不是针对成员用户填充的,所以我不能依靠它,尽管这似乎是我希望作为访客使用的.

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

更糟糕的是,我们确实有一个 member 用户,该用户也有一个EXT地址(其中mail not 填充的,而userPrincipalName是编码的),因此,仅基于成员类型使用一个或另一个属性也不正确.对我来说,解码和解析userPrincipalName似乎很脆弱且繁琐.

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.

推荐答案

我想从Microsoft Graph获取与Azure门户显示为用户名的值相同的值.

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

根据用户对象,使用Microsoft Graph Microsoft Graph 没有等同于用户名".

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

对我来说,解码和解析userPrincipalName似乎很脆弱而且很笨拙.

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

我不理解为什么解码和解析userPrincipalName对您来说似乎很脆弱且笨拙.实际上,这是获取用户名"的一种方法.而且我们可以轻松做到这一点.

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.

如果可以使用C#代码,则可以参考以下代码.

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

以下是整个演示代码

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

这篇关于如何获得等效的“用户名"使用Microsoft Graph的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆