简体   繁体   English

从 Microsoft graph 获取所有用户属性

[英]Get all user properties from Microsoft graph

We have an application which has used a local AD to fetch user info.我们有一个使用本地 AD 获取用户信息的应用程序。 Some customers want to move to the cloud and are using Azure AD.一些客户想要迁移到云端并使用 Azure AD。 We extended the app to sign users in via owin and now we're fetching users via Microsoft Graph.我们将应用程序扩展为通过 owin 登录用户,现在我们通过 Microsoft Graph 获取用户。

However from Microsoft Graph we do not get full user profiles.但是,我们无法从 Microsoft Graph 获得完整的用户配置文件。 We want to fetch all properties on users, not just the basic ones.我们想要获取用户的所有属性,而不仅仅是基本属性。

var client = new RestClient(string.Format("https://graph.microsoft.com/v1.0/users/{0}", userEmail));
request = new RestRequest();
request.Method = Method.GET;
request.AddHeader("Authorization", _token.Token);
var reponse = client.Execute(request);

This only gives me some information though, for example I don't get 'Department' from this.不过,这只会给我一些信息,例如我没有从中得到“部门”。 Is it possible to configure in azure what should be returned here, if so then where?是否可以在 azure 中配置这里应该返回什么,如果可以,那么在哪里? Or do I need something other than /users/ ?或者我还需要/users/以外的东西吗?

Different customers might have different special properties that need to be fetched.不同的客户可能有不同的特殊属性需要获取。 So the best solution would be to have an endpoint to call and get everything, including special properties not standard in azure ad.所以最好的解决方案是有一个端点来调用和获取所有东西,包括 azure 广告中不标准的特殊属性。 After that I can parse it on my side.之后,我可以在我这边解析它。 Is this possible?这可能吗?

The app has permission to read both basic and full profiles.该应用程序有权读取基本配置文件和完整配置文件。 Do I need something more?我需要更多的东西吗?

That's the normal behaviour of Graph API, see documentation here and this extract:这是 Graph API 的正常行为,请参阅此处的文档和此摘录:

By default, only a limited set of properties are returned ( businessPhones , displayName , givenName , id , jobTitle , mail , mobilePhone , officeLocation , preferredLanguage , surname , userPrincipalName ).默认情况下,只返回一组有限的属性( businessPhonesdisplayNamegivenNameidjobTitlemailmobilePhoneofficeLocationpreferredLanguagesurnameuserPrincipalName )。

To return an alternative property set, you must specify the desired set of user properties using the OData $select query parameter.要返回备用属性集,您必须使用 OData $select查询参数指定所需的用户属性集。 For example, to return displayName, givenName, and postalCode, you would use the add the following to your query $select=displayName,givenName,postalCode例如,要返回 displayName、givenName 和 postalCode,您可以使用将以下内容添加到您的查询$select=displayName,givenName,postalCode

You have to specify all fields in the select, as $select=* will only output the key fields in Graph API implementation.您必须在 select 中指定所有字段,因为$select=*只会输出 Graph API 实现中的关键字段。

So you will not be able to get what you ask (variable custom fields).所以你将无法得到你所要求的(可变的自定义字段)。

More info on the fields of User can be found here可以在此处找到有关用户字段的更多信息

User user = await graphServiceClient
    .Users[emailId]
    .Request()
    .Select(aadUser => new
    {
        aadUser.Id,
        aadUser.UserPrincipalName,
        aadUser.DisplayName,
        aadUser.GivenName,
        aadUser.Surname,
        aadUser.City,
        aadUser.MailNickname,
        aadUser.UserType
    })
    .GetAsync()
    .ConfigureAwait(false);

As already stated by NicolasR, you must list all the fields you want to retrieve by using the "$select" parameter;正如 NicolasR 所述,您必须使用“$select”参数列出您想要检索的所有字段; if you want, instead, to retrieve the custom fields, you can either add them to the previous parameter (if you know their names) or you can use "$expand=extensions"相反,如果您想检索自定义字段,可以将它们添加到上一个参数(如果您知道它们的名称),也可以使用“$expand=extensions”

function getGraphDataAdvanced($authToken, $urlGraph){
    $url = $urlGraph + '&$count=true'

    $data = (Invoke-RestMethod -Headers @{
        Authorization = "Bearer $($authToken)"
        ConsistencyLevel = "eventual"
    } -Uri $url -Method Get)

    $dataList = @()
    $dataList += $data.value

    $url = $data.'@Odata.NextLink'

    while ($null -ne $url){
        Write-Warning 'Retreiving Next Page'

        $data = (Invoke-RestMethod -Headers @{
            Authorization = "Bearer $($authToken)"
            ConsistencyLevel = "eventual"
        } -Uri $url -Method Get)

        $dataList += $data.value
        $url = $data.'@Odata.NextLink'
    }
    return $dataList
}

getGraphDataAdvanced $authToken 'https://graph.microsoft.com/beta/users? $expand=extensions'

Using the Microsoft Graph Explorer , I've been able to find all available properties for a user:使用Microsoft Graph Explorer ,我已经能够找到用户的所有可用属性:

  1. Go to "Groups" Go 到“组”
  2. Select "list all groups in my organization" Select“列出我组织中的所有组”
  3. Change the query to filter by a group you know and expand members : https://graph.microsoft.com/v1.0/groups?$filter=mail eq 'aGroup@company.com'&$expand=members更改查询以按您知道的组进行过滤并扩展成员https://graph.microsoft.com/v1.0/groups?$filter=mail eq 'aGroup@company.com'&$expand=members

Now you'll see all the available properties for the users.现在您将看到用户的所有可用属性。

I've been trying to find a way to get all Azure AD properties of objects via Powershell MSGraph cmdlets without it truncating at the right edge of the console.我一直在尝试找到一种方法来通过 Powershell MSGraph cmdlet 获取对象的所有 Azure AD 属性,而不会在控制台的右边缘截断。

I've discovered that Format-Custom triggers vomiting of (apparently) all properties of an object in a huge, alphabetical, indented, and bracketed list.我发现Format-Custom在一个巨大的、按字母顺序排列的缩进和括号列表中触发了(显然)object 的所有属性的呕吐。

Get-MgUser -filter "startswith(userprincipalname, 'username')" | format-custom

The formatted properties of a newly created and unused user account in Azure AD is 13217 lines long. Azure AD 中新创建且未使用的用户帐户的格式化属性为 13217 行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Microsoft Graph API 获取用户的照片(个人资料)? - How to get user's photo(profile) using Microsoft Graph API? 获取所有用户定义的 window 属性? - Get all user defined window properties? 如何从 microsoft graph 获取刷新令牌 - How do I get refresh token from microsoft graph 有没有办法从 Microsoft Graph 订阅响应中获取附件 url? - Is there a way to get Attachments url from Microsoft Graph Subscription response? 在React中从Firebase获取用户对象属性 - Get user object properties from Firebase in React Rhino:如何从ScriptableObject获取所有属性? - Rhino: How to get all properties from ScriptableObject? Microsoft图形客户端凭据-代表用户发送电子邮件的oauth错误 - microsoft graph client credentials - get oauth error sending email on behalf of user Microsoft Graph - 无法显示用户配置文件照片 - Microsoft Graph - Unable to Display User Profile Photos 使用Microsoft Graph获取用户的whenCreated属性 - Obtaining whenCreated property for user with Microsoft Graph 如何从 Microsoft graph API 访问用户的个人资料照片并显示 React 应用程序? - How to access user's profile photo from Microsoft graph API and display React application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM