azure - 如何使用 MS Graph 编程 API 检查服务主体的登录列表

标签 azure azure-active-directory

我可以使用 Microsoft Graph 编程 API 来检查所有 AAD 用户(包括服务主体)的登录情况。这是一个常用的功能:

https://learn.microsoft.com/en-us/graph/api/signin-list?view=graph-rest-beta&tabs=http#permissions

如果普通用户不是管理员,通常会禁止使用 API。此规则的异常(exception)是用户可以检查自己的登录。根据文档,它指出:

Note: Azure AD users with any permissions can read sign-in logs in which their user is the actor signing in

检查您自己的登录是通过过滤 userId 来完成的。我正在尝试使用服务主体的此功能来检查其自己的登录,如下所示:

https://graph.microsoft.com/beta/auditLogs/signIns $filter=(userId eq '123792d1-b50f-4f7f-a8a3-40e0341e9e22')&$top=10

在上面的示例中,“userId”是 AAD 服务主体的 oid。然而,当我对此进行测试时,API 不断失败并阻止我的服务检查其自己的登录。错误消息显示:

“调用主体没有所需的 MSGraph 权限 AuditLog.Read.All”

有人可以根据文档解释为什么 AAD 不允许服务主体检查自己的登录吗?

最佳答案

我创建了一个 Azure AD 应用程序,但未授予任何 API 权限,如下所示:

enter image description here

现在,我使用以下参数生成了访问 token :

https://login.microsoftonline.com/TenantID/oauth2/v2.0/token

client_id:ClientID
client_secret:ClientSecret
scope:https://graph.microsoft.com/.default
grant_type:client_credentials

enter image description here

当我尝试获取服务主体的审核日志时,我收到如下错误:

GET https://graph.microsoft.com/v1.0/auditLogs/signIns?$filter=(userId eq 'SPObjID')

enter image description here

注意:

  • 根据 MsDoc,任何具有适当权限的 Azure AD 用户都有权读取登录日志,其中他们的用户是登录参与者,但服务主体有一些异常(exception),可能不属于此类别。<
  • 为了建立登录身份和/或访问资源,需要使用服务主体。
  • 可以向服务主体授予访问权限和权限,但不能直接更新或更改它们。服务主体不属于能够查看登录日志(其用户是登录参与者)的用户。

因此,要解决该错误,请确保向管理员授予 AuditLog.Read.All API 权限:

enter image description here

分配 API 权限后,我能够成功获取审核日志,如下所示:

enter image description here

引用:

Get signIn - Microsoft Graph v1.0 | Microsoft Learn

关于azure - 如何使用 MS Graph 编程 API 检查服务主体的登录列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76368336/

相关文章:

azure - 通过 ARM 模板运行 powershell 命令会抛出 "The term ' Connect-AzureAD' 未被识别为 cmdlet 的名称”错误

azure - 将 Metamask 连接到 Azure 中的私有(private)区 block 链

ubuntu - 如何让我的 Stardog 服务在 azure ubuntu VM 中自动启动?

Azure 轻松身份验证 session 超时

azure - 有没有办法在 azure Active Directory 中为 Web 应用程序用户添加自定义权限

azure - 将许可证分配给 guest 用户

powershell - 将证书上传到 AAD 应用程序的 powershell 命令是什么?

azure - WebRole 中的证书列表为空

c# - 服务总线 - 单例连接类?

git - 当我尝试从 azure git 进行 git clone 时如何进行身份验证?