列出用户

命名空间:microsoft.graph

重要

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

检索 user 对象列表。

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

Permissions

要调用此 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。有关成员和来宾用户权限的详细信息,请参阅 Azure Active Directory 中的默认用户权限是什么?

HTTP 请求

GET /users

可选的查询参数

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

某些属性无法在用户集合中返回。以下属性仅在 检索单个用户 时受支持:aboutMebirthdayhireDateinterestsmySitepastProjectspreferredNameresponsibilitiesschoolsskillsmailboxSettings

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

检索扩展和关联数据

扩展类型 备注
onPremisesExtensionAttributes 1-15 默认返回。 支持 $filtereq)。
架构扩展 仅通过 $select 返回。 支持 $filtereq)。
开放扩展 仅通过 $expand 返回,即 users?$expand=extensions
目录扩展 默认返回。 支持 $filtereq)。

请求标头

标头
Authorization Bearer {token}(必需)
ConsistencyLevel 最终。 当使用 $search$filter 的特定用法时,需要此标头和 $count。 有关使用 ConsistencyLevel$count 的详细信息,请参阅 Azure AD 目录对象的高级查询功能

请求正文

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

响应

如果成功,此方法在响应正文中返回 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#@microsoft.onmicrosoft.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:列出用户,包括其上次登录时间

请求

以下是请求示例。有关" signInActivity 属性的详细信息,需要 Azure AD Premium P1/P2 许可证和 AuditLog.Read.All 权限。

注意: 检索 signInActivity 属性存在 已知问题

GET https://graph.microsoft.com/beta/users?$select=displayName,userPrincipalName,signInActivity

响应

下面展示了示例响应。

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

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

{
  "@odata.context": "https://graph.microsoft.com/beta/$metadata#users(displayName,userPrincipalName,signInActivity)",
  "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"
      }
    }
  ]
}

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

请求

以下是请求示例。有关" signInActivity 属性的详细信息,需要 Azure AD Premium P1/P2 许可证和 AuditLog.Read.All 权限。

注意: 检索 signInActivity 属性存在 已知问题

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 属性的详细信息,需要 Azure AD Premium P1/P2 许可证和 AuditLog.Read.All 权限。

注意: 检索 signInActivity 属性存在 已知问题

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 的详细信息,请参阅 Azure AD 目录对象的高级查询功能

注意:$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 的详细信息,请参阅 Azure AD 目录对象的高级查询功能

注意:$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#@microsoft.onmicrosoft.com"
    }
  ]
}

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

请求

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

注意:$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 的详细信息,请参阅 Azure AD 目录对象的高级查询功能

注意:$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 的详细信息,请参阅 Azure AD 目录对象的高级查询功能

注意:$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#@microsoft.onmicrosoft.com"
    }
  ]
}

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

请求

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

注意:$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 运算符。