如何使用 Microsoft Graph 分析活动日志

Microsoft Entra 报告 API 通过一组 REST API,让你以编程方式访问数据。 可从许多编程语言和工具中调用这些 API。

本文介绍如何使用 Microsoft Graph Explorer 和 Microsoft Graph PowerShell 分析 Microsoft Entra 活动日志。

先决条件

  • 一个正常运行的 Microsoft Entra 租户,且该租户关联了 Microsoft Entra ID P1 或 P2 许可证。
  • 要同意所需权限,需要成为特权角色管理员

使用 Microsoft Graph Explorer 访问报表

配置了所有先决条件后,可以在 Microsoft Graph 中运行活动日志查询。 Microsoft Graph API 不是为拉取大量活动数据而设计的。 使用 API 拉取大量活动数据可能会导致分页和性能问题。 有关 Microsoft Graph 活动日志查询的详细信息,请参阅活动报告 API 概述

  1. 启动 Microsoft Graph 浏览器工具

  2. 选择你的配置文件,然后选择“修改权限”。

  3. 同意以下所需权限:

    • AuditLog.Read.All
    • Directory.Read.All
  4. 使用以下查询之一开始使用 Microsoft Graph 访问活动日志:

    • GET https://graph.microsoft.com/v1.0/auditLogs/directoryAudits
    • GET https://graph.microsoft.com/v1.0/auditLogs/signIns
    • GET https://graph.microsoft.com/v1.0/auditLogs/provisioning
    • GET https://graph.microsoft.com/beta/auditLogs/signUps

    Microsoft Graph 中活动日志 GET 查询的屏幕截图。

微调查询

若要搜索特定活动日志条目,请使用 $filter 和 createdDateTime 查询参数和某个可用的属性。 以下某些查询使用 beta 终结点。 beta 终结点可能会更改,不建议将其用于生产。

示例登录查询

尝试使用以下查询进行登录活动:

  • 对于条件访问失败的登录尝试:

    • GET https://graph.microsoft.com/v1.0/auditLogs/signIns?$filter=conditionalAccessStatus eq 'failure'
    • 请考虑使用日期筛选器,这样请求不会超时。
  • 若要查找在特定时间段内登录到特定应用程序的情况,请执行以下操作:

    • GET https://graph.microsoft.com/v1.0/auditLogs/signIns?$filter=(createdDateTime ge 2024-01-13T14:13:32Z and createdDateTime le 2024-01-14T17:43:26Z) and appId eq 'APP ID'
  • 对于非交互式登录:

    • GET https://graph.microsoft.com/beta/auditLogs/signIns?$filter=(createdDateTime ge 2024-01-13T14:13:32Z and createdDateTime le 2024-01-14T17:43:26Z) and signInEventTypes/any(t: t eq 'nonInteractiveUser')
  • 对于服务主体登录:

    • GET https://graph.microsoft.com/beta/auditLogs/signIns?$filter=(createdDateTime ge 2024-01-13T14:13:32Z and createdDateTime le 2024-01-14T17:43:26Z) and signInEventTypes/any(t: t eq 'servicePrincipal')
  • 对于托管标识登录:

    • GET https://graph.microsoft.com/beta/auditLogs/signIns?$filter=(createdDateTime ge 2024-01-13T14:13:32Z and createdDateTime le 2024-01-14T17:43:26Z) and signInEventTypes/any(t: t eq 'managedIdentity')
  • 获取用户的身份验证方法:

    • GET https://graph.microsoft.com/beta/users/{userObjectId}/authentication/methods
    • 需要 UserAuthenticationMethod.Read.All 权限
  • 查看用户注册详细信息报告:

    • GET https://graph.microsoft.com/beta/reports/authenticationMethods/userRegistrationDetails
    • 需要 UserAuthenticationMethod.Read.All 权限
  • 有关特定用户的注册详细信息:

    • GET https://graph.microsoft.com/beta/reports/authenticationMethods/userRegistrationDetails/{userId}
    • 需要 UserAuthenticationMethod.Read.All 权限

示例注册查询(预览版)

尝试使用以下查询查看 外部租户中的注册活动:

  • 若要查找在特定步骤中失败或中断的注册尝试,例如用户对象创建:

    • GET https://graph.microsoft.com/beta/auditLogs/signUps?$filter=status/errorCode ne 0 and signUpStage eq 'userCreation'
  • 若要查找在电子邮件验证期间失败的注册尝试,

    • GET https://graph.microsoft.com/beta/auditLogs/signUps?$filter=status/errorCode eq 1002013 and signUpStage eq 'credentialValidation'

    注释

    错误代码1002013指示注册流的预期(且成功)中断。 了解详细信息

  • 对于在特定日期范围内的注册:

    • GET https://graph.microsoft.com/beta/auditLogs/signUps?&$filter=(createdDateTime ge 2024-01-13T14:13:32Z and createdDateTime le 2024-01-14T17:43:26Z)
  • 对于某个应用程序的注册:

    • GET https://graph.microsoft.com/beta/auditLogs/signUps?$filter=appId eq 'AppId'
  • 对于本地帐户注册:

    • GET https://graph.microsoft.com/beta/auditLogs/signUps?$filter=signUpIdentityProvider eq 'Email OTP' or signUpIdentityProvider eq 'Email Password'
  • 对于社交帐户注册(本示例中的 Google):

    • GET https://graph.microsoft.com/beta/auditLogs/signUps?$filter=signUpIdentityProvider eq ‘Google'
  • 若要查看特定用户的条目,例如 user@contoso.com

    • GET https://graph.microsoft.com/beta/auditLogs/signUps?$filter=signUpIdentity/signUpIdentifier eq 'user@contoso.com'
  • 若要查找与特定关联 ID 匹配的条目:

    • GET https://graph.microsoft.com/beta/auditLogs/signUps?$filter=correlationId eq 'CorrelationId'
  • 若要查找与特定注册对应的登录日志条目,请使用相关 ID:

    • GET https://graph.microsoft.com/v1.0/auditLogs/signIns?$filter=correlationId eq 'CorrelationId'

熟悉标准登录和审核日志后,请尝试浏览以下其他 API:

使用 Microsoft Graph PowerShell 访问报表

可以使用 PowerShell 访问 Microsoft Entra 报告 API。 有关详细信息,请参阅 Microsoft Graph PowerShell 概述

Microsoft Graph PowerShell cmdlet:

常见错误

错误:两个租户都不是 B2C 或租户没有高级许可证:访问登录报告需要 Microsoft Entra ID P1 或 P2 许可证。 如果访问登录时看到此错误消息,请确保你的租户已获得 Microsoft Entra ID P1 许可证。

错误:用户没有允许的角色:如果在尝试使用 API 访问审核日志或登录时看到此错误消息,请确保你的帐户属于 Microsoft Entra 租户中的安全读取者报告读取者角色。

错误:应用程序缺少 Microsoft Entra ID“读取目录数据”或“读取所有审核日志数据”权限:应用程序必须具有 AuditLog.Read.AllDirectory.Read.All 权限才能使用 Microsoft Graph 访问活动日志。