powershell - 缺少 Microsoft Graph ServicePrincipal

标签 powershell azure azure-active-directory azure-ad-graph-api

TL;TR 我们正在使用 Microsoft Graph API 创建 AAD 应用程序。该应用程序有一些 requiredResourceAccess 条目,其中需要访问 microsoft graph。 创建应用程序后,我们希望使用 appRoleAssignments 对象将角色分配给服务主体。该对象需要 resourceId,这是我尝试确定的 objectId(例如 microsoft graph)。

我们使用 Graph API 本身来获取服务主体: https://graph.windows.net/<tenant>/servicePrincipals?api-version=1.6但不知何故 Microsoft Graph 丢失了:

Windows Azure Active Directory      
Microsoft App Access Panel          
Azure Classic Portal                
Microsoft.SMIT                      
Office 365 Configure                
Windows Azure Service Management API
Microsoft.SupportTicketSubmission   
Azure ESTS Service                  
Signup                              
Microsoft password reset service  
<小时/>

我需要确定 Microsoft Graph 服务主体ObjectId。从新的 AAD 开始,似乎没有 Microsoft Graph 主体:

Get-MsolServicePrincipal -AppPrincipalId 00000003-0000-0000-c000-000000000000

输出

Get-MsolServicePrincipal : Service principal was not found.

如何确定 Microsoft Graph 的 ObjectId(最好使用graph.windows.net API)?

<小时/>

编辑1:

按照 Fei Xu 的建议,使用以下方法通过 Rest 创建服务主体:

POST: https://graph.windows.net/{tenantId}/servicePrincipals?api-version=1.6

Authorization: Bearer {access_token}

{
  "appId": "00000003-0000-0000-c000-000000000000",
  "accountEnabled": true
}

给我一​​个400(错误请求)错误代码:

enter image description here

最佳答案

I need to determine the ObjectId of the Microsoft Graph Service Principal. Starting with a fresh AAD, it seems like there is no Microsoft Graph Principal:

在用户授予应用程序同意后,将创建在其他租户上注册的 Multi-Tenancy 应用程序(Microsoft Graph)的服务主体。这就是为什么您无法在新租户中找到它的原因。

要获取Microsoft Graph的对象id,您需要注册并授予Microsoft Graph的权限,如下图:

enter image description here

之后,Get-MsolServicePrincipal 命令应该可以为您工作(注意:授予权限后您可能需要等待几秒钟)。

更详细的服务主体,可以引用this document .

更新

POST: https://graph.windows.net/{tenantId}/servicePrincipals?api-version=1.6

Authorization: Bearer {access_token}

{
  "appId": "00000003-0000-0000-c000-000000000000",
  "accountEnabled": true
}

更新2

上述 REST 使用在 Microsoft 租户上注册的应用程序 (1950a258-227b-4e31-a9cf-717495945fc2) 来获取 token 。要切实地为 Microsoft Graph 创建服务主体,我们可以调用 New-AzureRMADServicePrincipal 命令。

这是一个适合我的 C# 代码示例:

try
{
    var userName = "";
    var password = "";
    var securePassword = new SecureString();
    foreach (char c in password)
    {
        securePassword.AppendChar(c);
    }

    // Create Initial Session State for runspace.
    InitialSessionState initialSession = InitialSessionState.CreateDefault();
    // Create credential object.
    PSCredential credential = new PSCredential(userName, securePassword);
    // Create command to Log in to Azure.
    Command connectCommand = new Command("Login-AzureRmAccount");
    connectCommand.Parameters.Add((new CommandParameter("Credential", credential)));
    // Create command to create service principal.
    Command createSP = new Command("New-AzureRMADServicePrincipal");
    createSP.Parameters.Add(new CommandParameter("ApplicationId", "00000003-0000-0000-c000-000000000000"));
    using (Runspace psRunSpace = RunspaceFactory.CreateRunspace(initialSession))
    {
        // Open runspace.
        psRunSpace.Open();

        //Iterate through each command and executes it.
        foreach (var com in new Command[] { connectCommand, createSP})
        {
            var pipe = psRunSpace.CreatePipeline();
            pipe.Commands.Add(com);
            pipe.Invoke();

        }
        // Close the runspace.
        psRunSpace.Close();
    }
}
catch (Exception)
{
    throw;
}

关于powershell - 缺少 Microsoft Graph ServicePrincipal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43273869/

相关文章:

azure - 是否可以使用 Azure Active Directory 身份验证在本地调试 Azure Functions?

powershell - 将变量串在一起以清除它们是否可以?

用于停止和启动 Azure VM 的 Powershell

c# - 如何使用 C# 连接到 Blob 存储容器

azure - 获取 Azure AD 主体的所有角色分配

azure - 需要对 Azure AD 和 ADFS 上的黑白 SAML 配置进行比较

powershell - 修剪角色右侧

powershell - 并行化 powershell 脚本执行

linux - Azure Web应用程序: 'docker: command not found' after I installed docker

azure - 如何使用 prependpath 在 azure-pipeline 中设置路径