如何使用 Microsoft Graph 分析活動記錄

Microsoft Entra 報告 API 可讓您透過一組 REST API 以程式設計方式存取數據。 您可以從許多程式設計語言和工具呼叫這些 API。 Microsoft Graph API 並非專為提取大量活動數據而設計。 使用 API 提取大量的活動數據可能會導致分頁和效能發生問題。

本文說明如何使用 Microsoft Graph Explorer 和 Microsoft Graph PowerShell 分析 Microsoft Entra 活動記錄。

必要條件

若要對 Microsoft Graph API 提出要求,您必須先:

使用 Microsoft Graph 託管存取報表

設定所有必要條件后,您就可以在 Microsoft Graph 中執行活動記錄查詢。 如需活動記錄的 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

    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 權限

一旦您熟悉標準登入和稽核記錄,請嘗試探索下列其他 API:

使用 Microsoft Graph PowerShell 存取報表

您可以使用 PowerShell 來存取 Microsoft Entra 報告 API。 如需詳細資訊,請參閱 Microsoft Graph PowerShell 概觀

Microsoft Graph PowerShell Cmdlet:

  • 稽核記錄檔Get-MgAuditLogDirectoryAudit
  • 登入記錄檔Get-MgAuditLogSignIn
  • 布建記錄:Get-MgAuditLogProvisioning
  • 探索報告相關 Microsoft Graph PowerShell Cmdlet 的完整清單

常見錯誤

錯誤:租使用者不是 B2C 或租用戶沒有進階授權:存取登入報告需要 Microsoft Entra ID P1 或 P2 授權。 如果您在存取登入時看到此錯誤訊息,請確定您的租用戶已獲得 Microsoft Entra ID P1 授權的授權。

錯誤:使用者不在允許的角色中:如果您在嘗試使用 API 存取稽核記錄或登入時看到此錯誤訊息,請確定您的帳戶是 Microsoft Entra 租使用者中安全性讀取者報表讀取者角色的一部分。

錯誤:應用程式遺漏 Microsoft Entra ID 'Read directory data' 或 'Read all audit log data' 許可權:應用程式必須具有 AuditLog.Read.AllDirectory.Read.All 許可權,才能使用 Microsoft Graph 存取活動記錄。

下一步