问题描述
我想从 Microsoft Graph 中获取与 Azure 门户显示为用户名相同的值(如下所示):
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 Microsoft Graph 的用户名".
According to user object, there is no equivalent of "user name" using Microsoft Graph Microsoft Graph.
对我来说,解码和解析 userPrincipalName 似乎很脆弱.
我不明白为什么解码和解析 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 图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!