列出用户

命名空间:microsoft.graph

重要

Microsoft Graph /beta 版本下的 API 可能会发生更改。 不支持在生产应用程序中使用这些 API。 若要确定 API 是否在 v1.0 中可用,请使用 版本 选择器。

检索 user 对象列表。

默认情况下,此操作仅返回每位用户较常用属性中的一部分。 这些默认属性将记录在属性部分中。 要获取默认返回的属性,请对用户执行 GET 操作,并在 $select OData 查询选项中指定这些属性。

此 API 可用于以下国家级云部署

全局服务 美国政府 L4 美国政府 L5 (DOD) 由世纪互联运营的中国

权限

要调用此 API,需要以下权限之一。 若要了解详细信息,包括如何选择权限的信息,请参阅权限

权限类型 权限(从最低特权到最高特权)
委派(工作或学校帐户) User.ReadBasic.All、User.Read.All、User.ReadWrite.All、Directory.Read.All、Directory.ReadWrite.All
委派(个人 Microsoft 帐户) 不支持。
应用程序 User.Read.All、User.ReadWrite.All、Directory.Read.All、Directory.ReadWrite.All

来宾用户无法调用此 API。 有关成员和来宾用户的权限的详细信息,请参阅 Microsoft Entra ID 中的默认用户权限是什么?

HTTP 请求

GET /users

可选的查询参数

此方法支持 $count、、$expand$filter$orderby$search$select$topOData 查询参数来帮助自定义响应。 不支持 $skip。 默认和最大页面大小分别为 100 和 999 个用户对象,但指定 $select=signInActivity$filter=signInActivity时除外。 signInActivity选择或筛选后,最大页面大小为 999。 只有将 ConsistencyLevel 标头设置为 eventual$count 时,才支持某些查询。 有关详细信息,请参阅 目录对象的高级查询功能$count$search 参数当前在 Azure AD B2C 租户中不可用。

扩展属性 还支持查询参数,在某些情况下,仅支持高级查询参数。 有关详细信息,请参阅 $filter by 扩展属性的支持

某些属性无法在用户集合中返回。 仅 当检索单个用户时,才支持以下属性: aboutMe生日hireDateinterestsmySitepastProjectspreferredName责任学校技能mailboxSettings

个人 Microsoft 帐户不支持下列属性,且将为 nullaboutMebirthdayinterestsmySitepastProjectspreferredNameresponsibilitiesschoolsskillsstreetAddress

请求头

标头
Authorization 持有者 {token}。 必填。 详细了解 身份验证和授权
ConsistencyLevel 最终。 当使用 $search$filter 的特定用法时,需要此标头和 $count。 有关使用 ConsistencyLevel$count的详细信息,请参阅 目录对象的高级查询功能

请求正文

请勿提供此方法的请求正文。

响应

如果成功,此方法在响应正文中返回 200 OK 响应代码和 user 对象集合。

尝试使用 $select/userscollection 中检索无法在用户集合中返回的属性(例如, 请求../users?$select=aboutMe)返回 501 Not Implemented 错误代码。

示例

示例 1:列出所有用户

请求

以下示例显示了一个请求。

GET https://graph.microsoft.com/beta/users

响应

以下示例显示了相应的响应。

注意:为了提高可读性,可能缩短了此处显示的响应对象。

HTTP/1.1 200 OK
Content-type: application/json

{
  "value":[
    {
      "displayName":"contoso1",
      "mail":"'contoso1@gmail.com",
      "mailNickname":"contoso1_gmail.com#EXT#",
      "otherMails":["contoso1@gmail.com"],
      "proxyAddresses":["SMTP:contoso1@gmail.com"],
      "userPrincipalName":"contoso1_gmail.com#EXT#@contoso.com"
    }
  ]
}

示例 2:使用登录名创建用户帐户

使用登录名(也称为本地帐户)查找用户帐户。

注意: 筛选 issuerAssignedId时,必须同时提供 颁发者issuerAssignedId。 但是,在某些情况下将忽略 颁发者 值。 有关对标识进行筛选的详细信息,请参阅 objectIdentity 资源类型

请求

以下示例显示了一个请求。

GET https://graph.microsoft.com/beta/users?$select=displayName,id&$filter=identities/any(c:c/issuerAssignedId eq 'j.smith@yahoo.com' and c/issuer eq 'My B2C tenant')

响应

以下示例显示了相应的响应。

注意:为了提高可读性,可能缩短了此处显示的响应对象。

HTTP/1.1 200 OK
Content-type: application/json

{
  "value": [
    {
      "displayName": "John Smith",
      "id": "87d349ed-44d7-43e1-9a83-5f2406dee5bd"
    }
  ]
}

示例 3:通过 userPrincipalName 从特定租户或域获取来宾 (B2B) 用户

请求

以下示例显示了一个请求。 来宾 (B2B 协作的 userPrincipalName 值) 用户始终包含“#EXT#”标识符。 例如,其主租户中用户的 userPrincipalName 为 AdeleV@adatum.com。 邀请用户在租户中协作时, contoso.com 租户中的 userPrincipalName 为“AdeleV_adatum.com#EXT#@contoso.com”。

此请求需要 将 ConsistencyLevel 标头设置为 eventual$count=true 查询字符串,因为请求包含 endsWith 运算符。 有关使用 ConsistencyLevel$count的详细信息,请参阅 目录对象的高级查询功能

注意: 必须在请求 URL 中将 userPrincipalName 值中的保留字符“#”编码为“%23”。 有关详细信息,请参阅 编码特殊字符

GET https://graph.microsoft.com/beta/users?$select=id,displayName,mail,identities&$filter=endsWith(userPrincipalName,'%23EXT%23@contoso.com')&$count=true
ConsistencyLevel: eventual

响应

以下示例显示了相应的响应。

注意:为了提高可读性,可能缩短了此处显示的响应对象。

HTTP/1.1 200 OK
Content-type: application/json

{
    "@odata.context": "https://graph.microsoft.com/beta/$metadata#users(id,displayName,mail,identities)",
    "@odata.count": 2,
    "value": [
        {
            "id": "39807bd1-3dde-48f3-8165-81ddd4e46de0",
            "displayName": "Adele Vance",
            "mail": "AdeleV@adatum.com",
            "identities": [
                {
                    "signInType": "userPrincipalName",
                    "issuer": "contoso.com",
                    "issuerAssignedId": "AdeleV_adatum.com#EXT#@cntoso.com"
                }
            ]
        }
    ]
}

示例 4:列出具有特定显示名称的用户的上次登录时间

请求

以下示例显示了一个请求。 signInActivity 属性的详细信息需要Microsoft Entra ID P1 或 P2 许可证以及 AuditLog.Read.All 权限。

GET https://graph.microsoft.com/beta/users?$filter=startswith(displayName,'Eric')&$select=displayName,signInActivity

响应

以下示例显示了相应的响应。

注意:为了提高可读性,可能缩短了此处显示的响应对象。

HTTP/1.1 200 OK
Content-type: application/json

{
  "@odata.context": "https://graph.microsoft.com/beta/users?$filter=startswith(displayName,'Eric')&$select=displayName,signInActivity",
  "value": [
    {
      "displayName": "Eric Solomon",
      "signInActivity": {
        "lastSignInDateTime": "2021-07-29T15:53:27Z",
        "lastSignInRequestId": "f3149ee1-e347-4181-b45b-99a1f82b1c00",
        "lastNonInteractiveSignInDateTime": "2021-07-29T17:53:42Z",
        "lastNonInteractiveSignInRequestId": "868efa6a-b2e9-40e9-9b1c-0aaea5b50200"
      }
    }
  ]
}

示例 5:列出用户在特定时间范围内的上次登录时间

请求

以下示例显示了一个请求。 signInActivity 属性的详细信息需要Microsoft Entra ID P1 或 P2 许可证以及 AuditLog.Read.All 权限。

GET https://graph.microsoft.com/beta/users?filter=signInActivity/lastSignInDateTime le 2021-07-21T00:00:00Z

响应

以下示例显示了相应的响应。

注意:为了提高可读性,可能缩短了此处显示的响应对象。

HTTP/1.1 200 OK
Content-type: application/json

{
  "@odata.context": "https://graph.microsoft.com/beta/users?filter=signInActivity/lastSignInDateTime le 2021-07-21T00:00:00Z",
  "value": [
    {
      "displayName": "Adele Vance",
      "userPrincipalName": "AdeleV@contoso.com",
      "signInActivity": {
        "lastSignInDateTime": "2021-06-17T16:41:33Z",
        "lastSignInRequestId": "d4d31c40-4c36-4775-ad59-7d1e6a171f00",
        "lastNonInteractiveSignInDateTime": "0001-01-01T00:00:00Z",
        "lastNonInteractiveSignInRequestId": ""
      }
    },
    {
      "displayName": "Alex Wilber",
      "userPrincipalName": "AlexW@contoso.com",
      "signInActivity": {
        "lastSignInDateTime": "2021-07-29T15:53:27Z",
        "lastSignInRequestId": "f3149ee1-e347-4181-b45b-99a1f82b1c00",
        "lastNonInteractiveSignInDateTime": "2021-07-29T17:53:42Z",
        "lastNonInteractiveSignInRequestId": "868efa6a-b2e9-40e9-9b1c-0aaea5b50200"
      }
    }
  ]
}

示例 6:仅获取用户数量

请求

以下示例显示了一个请求。 此请求要求将 ConsistencyLevel 标头设置为 eventual,因为在请求中有 $count。 有关使用 ConsistencyLevel$count的详细信息,请参阅 目录对象的高级查询功能

注意:$count$search 查询参数当前在 Azure AD B2C 租户中不可用。

GET https://graph.microsoft.com/beta/users/$count
ConsistencyLevel: eventual

响应

以下示例显示了相应的响应。

HTTP/1.1 200 OK
Content-type: text/plain

893

示例 7:使用 $filter 和 $top 获取显示名称以“a”开头(包括返回的对象数)的组。

请求

以下示例显示了一个请求。 此请求需要将 ConsistencyLevel 标头设置为 eventual$count=true 查询字符串,因为请求同时具有 $orderby$filter 查询参数。 有关使用 ConsistencyLevel$count的详细信息,请参阅 目录对象的高级查询功能

注意:$count$search 查询参数当前在 Azure AD B2C 租户中不可用。

GET https://graph.microsoft.com/beta/users?$filter=startswith(displayName,'a')&$orderby=displayName&$count=true&$top=1
ConsistencyLevel: eventual

响应

以下示例显示了相应的响应。

注意:为了提高可读性,可能缩短了此处显示的响应对象。

HTTP/1.1 200 OK
Content-type: application/json

{
  "@odata.context":"https://graph.microsoft.com/beta/$metadata#users",
  "@odata.count":1,
  "value":[
    {
      "displayName":"a",
      "mail":"a@contoso.com",
      "mailNickname":"a_contoso.com#EXT#",
      "otherMails":["a@contoso.com"],
      "proxyAddresses":["SMTP:a@contoso.com"],
      "userPrincipalName":"a_contoso.com#EXT#@contoso.com"
    }
  ]
}

示例 8:使用 $filter 获取所有以“”a@contoso.com结尾的邮件的用户,包括返回的对象计数,结果按 userPrincipalName 排序

请求

以下示例显示了一个请求。 此请求需要将 ConsistencyLevel 标头设置为 eventual$count=true 查询字符串,因为请求同时具有 $orderby$filter 查询参数,并且还使用 endsWith 运算符。 有关使用 ConsistencyLevel$count的详细信息,请参阅 目录对象的高级查询功能

注意:$count$search 查询参数当前在 Azure AD B2C 租户中不可用。

GET https://graph.microsoft.com/beta/users?$filter=endswith(mail,'a@contoso.com')&$orderby=userPrincipalName&$count=true
ConsistencyLevel: eventual

响应

以下示例显示了相应的响应。

注意:为了提高可读性,可能缩短了此处显示的响应对象。

HTTP/1.1 200 OK
Content-type: application/json

{
  "@odata.context": "https://graph.microsoft.com/beta/$metadata#users",
  "@odata.count": 1,
  "value": [
    {
      "displayName": "Grady Archie",
      "givenName": "Grady",
      "jobTitle": "Designer",
      "mail": "GradyA@contoso.com",
      "userPrincipalName": "GradyA@contoso.com",
      "id": "e8b753b5-4117-464e-9a08-713e1ff266b3"
      }
    ]
}

示例 9:使用 $search 获取显示名称中包含字母“wa”(包括返回的对象数)的用户。

请求

以下示例显示了一个请求。 此请求要求将 ConsistencyLevel 标头设置为 eventual,因为在请求中有 $search。 有关使用 ConsistencyLevel$count的详细信息,请参阅 目录对象的高级查询功能

注意:$count$search 查询参数当前在 Azure AD B2C 租户中不可用。

GET https://graph.microsoft.com/beta/users?$search="displayName:wa"&$orderby=displayName&$count=true
ConsistencyLevel: eventual

响应

以下示例显示了相应的响应。

注意:为了提高可读性,可能缩短了此处显示的响应对象。

HTTP/1.1 200 OK
Content-type: application/json

{
  "@odata.context":"https://graph.microsoft.com/beta/$metadata#users",
  "@odata.count":7,
  "value":[
    {
      "displayName":"Oscar Ward",
      "givenName":"Oscar",
      "mail":"oscarward@contoso.com",
      "mailNickname":"oscward",
      "userPrincipalName":"oscarward@contoso.com"
    }
  ]
}

示例 10:使用 $search 获取显示名称中包含字母"wa"或字母"ad"(包括返回对象计数)的用户

请求

以下示例显示了一个请求。 此请求要求将 ConsistencyLevel 标头设置为 eventual,因为在请求中有 $search。 有关使用 ConsistencyLevel$count的详细信息,请参阅 目录对象的高级查询功能

注意:$count$search 查询参数当前在 Azure AD B2C 租户中不可用。

GET https://graph.microsoft.com/beta/users?$search="displayName:wa" OR "displayName:ad"&$orderby=displayName&$count=true
ConsistencyLevel: eventual

响应

以下示例显示了相应的响应。

注意:为了提高可读性,可能缩短了此处显示的响应对象。

HTTP/1.1 200 OK
Content-type: application/json

{
  "@odata.context":"https://graph.microsoft.com/beta/$metadata#users",
  "@odata.count":7,
  "value":[
    {
      "displayName":"Oscar Ward",
      "givenName":"Oscar",
      "mail":"oscarward@contoso.com",
      "mailNickname":"oscward",
      "userPrincipalName":"oscarward@contoso.com"
    },
    {
      "displayName":"contosoAdmin1",
      "mail":"'contosoadmin1@gmail.com",
      "mailNickname":"contosoadmin1_gmail.com#EXT#",
      "proxyAddresses":["SMTP:contosoadmin1@gmail.com"],
      "userPrincipalName":"contosoadmin1_gmail.com#EXT#@contoso.com"
    }
  ]
}

示例 11:使用 $filter为用户分配特定许可证

请求

以下示例显示了一个请求。 此请求要求将 ConsistencyLevel 标头设置为 eventual,因为在请求中有 $search。 有关使用 ConsistencyLevel$count的详细信息,请参阅 目录对象的高级查询功能

注意:$count$search 查询参数当前在 Azure AD B2C 租户中不可用。

GET https://graph.microsoft.com/beta/users?$select=id,mail,assignedLicenses&$filter=assignedLicenses/any(u:u/skuId eq cbdc14ab-d96c-4c30-b9f4-6ada7cdc1d46)

响应

以下示例显示了相应的响应。

HTTP/1.1 200 OK
Content-type: application/json

{
  "@odata.context": "https://graph.microsoft.com/beta/$metadata#users(id,mail,assignedLicenses)",
  "value": [
    {
      "id": "cb4954e8-467f-4a6d-a8c8-28b9034fadbc",
      "mail": "admin@contoso.com",
      "assignedLicenses": [
        {
          "disabledPlans": [],
          "skuId": "cbdc14ab-d96c-4c30-b9f4-6ada7cdc1d46"
        }
      ]
    },
    {
      "id": "81a133c2-bdf2-4e67-8755-7264366b04ee",
      "mail": "DebraB@contoso.com",
      "assignedLicenses": [
        {
          "disabledPlans": [],
          "skuId": "cbdc14ab-d96c-4c30-b9f4-6ada7cdc1d46"
        }
      ]
    }
  ]
}

示例 12:获取所有用户的架构扩展值

在此示例中,架构扩展 ID 为 ext55gb1l09_msLearnCourses

请求

GET https://graph.microsoft.com/beta/users?$select=ext55gb1l09_msLearnCourses

响应

在以下响应中,在两个用户对象未分配架构扩展属性 ext55gb1l09_msLearnCourses

HTTP/1.1 200 OK
Content-type: application/json

{
    "@odata.context": "https://graph.microsoft.com/beta/$metadata#users(ext55gb1l09_msLearnCourses)",
    "value": [
        {},
        {
            "ext55gb1l09_msLearnCourses": {
                "@odata.type": "#microsoft.graph.ComplexExtensionValue",
                "courseType": "Developer",
                "courseName": "Introduction to Microsoft Graph",
                "courseId": 1
            }
        },
        {}
    ]
}

请注意: 还可以对架构扩展属性应用 $filter 以检索集合中属性与指定值匹配的对象。 语法为 /users?$filter={schemaPropertyID}/{propertyName} eq 'value'。 例如,GET /users?$select=ext55gb1l09_msLearnCourses&$filter=ext55gb1l09_msLearnCourses/courseType eq 'Developer'。 支持 eqnot 运算符。

示例 13:列出自定义安全属性分配等于值的所有用户

以下示例演示如何列出具有等于值的自定义安全属性分配的所有用户。 该示例检索具有名为 AppCountry 且值等于 Canada的自定义安全属性的用户。 筛选器值区分大小写。 必须在 ConsistencyLevel=eventual 请求或标头中添加 。 还必须包含 $count=true 以确保正确路由请求。

用户 #1

  • 属性集:Marketing
  • 属性:AppCountry
  • 属性数据类型:字符串集合
  • 属性值:["India","Canada"]

用户 #2

  • 属性集:Marketing
  • 属性:AppCountry
  • 属性数据类型:字符串集合
  • 属性值:["Canada","Mexico"]

若要获取自定义安全属性分配,必须为调用主体分配“属性分配读取者”或“属性分配管理员”角色,并且必须被授予 CustomSecAttributeAssignment.Read.AllCustomSecAttributeAssignment.ReadWrite.All 权限。

有关自定义安全属性分配的示例,请参阅示例:使用 Microsoft 图形 API分配、更新、列出或删除自定义安全属性分配

请求

GET https://graph.microsoft.com/beta/users?$count=true&$select=id,displayName,customSecurityAttributes&$filter=customSecurityAttributes/Marketing/AppCountry eq 'Canada'
ConsistencyLevel: eventual

响应

HTTP/1.1 200 OK

{
    "@odata.context": "https://graph.microsoft.com/beta/$metadata#users(id,displayName,customSecurityAttributes)",
    "@odata.count": 2,
    "value": [
        {
            "id": "dbaf3778-4f81-4ea0-ac1c-502a293c12ac",
            "displayName": "Jiya",
            "customSecurityAttributes": {
                "Engineering": {
                    "@odata.type": "#microsoft.graph.customSecurityAttributeValue",
                    "Datacenter@odata.type": "#Collection(String)",
                    "Datacenter": [
                        "India"
                    ]
                },
                "Marketing": {
                    "@odata.type": "#microsoft.graph.customSecurityAttributeValue",
                    "AppCountry@odata.type": "#Collection(String)",
                    "AppCountry": [
                        "India",
                        "Canada"
                    ],
                    "EmployeeId": "KX19476"
                }
            }
        },
        {
            "id": "6bac433c-48c6-4213-a316-1428de32701b",
            "displayName": "Jana",
            "customSecurityAttributes": {
                "Marketing": {
                    "@odata.type": "#microsoft.graph.customSecurityAttributeValue",
                    "AppCountry@odata.type": "#Collection(String)",
                    "AppCountry": [
                        "Canada",
                        "Mexico"
                    ],
                    "EmployeeId": "GS46982"
                }
            }
        }
    ]
}

示例 14:列出其管理受到限制的所有用户

请求

GET https://graph.microsoft.com/beta/users?$filter=isManagementRestricted eq true&$select=displayName,userPrincipalName

响应

注意:为了提高可读性,可能缩短了此处显示的响应对象。

HTTP/1.1 200 OK
Content-type: application/json

{
    "@odata.context": "https://graph.microsoft.com/beta/$metadata#users(displayName,userPrincipalName)",
    "value": [
        {
            "displayName": "Adele",
            "userPrincipalName": "Adele@contoso.com"
        },
        {
            "displayName": "Bob",
            "userPrincipalName": "Bob@contoso.com"
        }
    ]
}