将组织结构图添加到 Azure Active Directory 令牌声明

问题描述 投票:0回答:1

当 Azure Active Directory 颁发令牌声明时,我需要将组织结构图信息包含到令牌声明中。

假设我的公司有这样的组织:

例如,当我以“Roary Moody”身份登录时,我希望收到“Francis Jefferson”是我的 CFO 的令牌。

有什么办法可以做到这一点吗?

azure azure-active-directory active-directory
1个回答
0
投票

通常,您可以运行下面的 Microsoft Graph API 调用,通过展开 manager 属性来获取组织结构图中登录用户的用户层次结构详细信息:

GET https://graph.microsoft.com/v1.0/me?$expand=manager($select=id,displayName,jobTitle)

回复:

enter image description here

或者,您可以通过运行以下示例 PowerShell 脚本来创建声明映射策略,以在令牌的自定义声明中获取相同的值:

#Install-Module Microsoft.Graph
Connect-MgGraph
$policyDefinition = @{
    definition = '{
        "ClaimsMappingPolicy": {
            "Version": 1,
            "IncludeBasicClaimSet": true,
            "ClaimsSchema": [
                {"Source": "user", "ID": "extensionattribute1", "SamlClaimType": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/manager", "JwtClaimType": "manager"}
            ]
        }
    }'
    displayName = "AddManager"
}

$Policy = New-MgPolicyClaimMappingPolicy -BodyParameter $policyDefinition
Get-MgPolicyClaimMappingPolicy -ClaimsMappingPolicyId $Policy.Id | fl

回复:

enter image description here

现在,将此策略分配给您使用以下命令生成令牌的服务主体

$servicePrincipalId = "spObjectId" 
$policyId = "policyId" 

$params = @{
    "@odata.id" = "https://graph.microsoft.com/v1.0/policies/claimsMappingPolicies/$policyId"
}

New-MgServicePrincipalClaimMappingPolicyByRef -ServicePrincipalId $servicePrincipalId -BodyParameter $params
Get-MgServicePrincipalClaimMappingPolicy -ServicePrincipalId $servicePrincipalId | fl

回复:

enter image description here

现在,通过运行以下命令更新 manager 在用户属性中的声明值:

$userId = "userId"

$params = @{
    onPremisesExtensionAttributes = @{
        extensionAttribute1 = "ManagerName(jobTitle)"
    }
}
Update-MgUser -UserId $userId -BodyParameter $params

回复:

enter image description here

确保在应用程序注册清单中启用以下设置:

enter image description here

现在,我通过在应用程序注册中添加自定义范围来公开 API,如下所示:

enter image description here

在我的例子中,我使用隐式流来生成访问令牌,应启用以下选项:

enter image description here

当我通过使用更新的用户登录在浏览器中运行以下授权 URL 时,我通过 manager 成功获得了令牌,如下所示:

https://login.microsoftonline.com/tenantID/oauth2/v2.0/authorize?client_id=appID&response_type=token&redirect_uri=https://jwt.ms&scope=api://appID/custom.read&state=12345&nonce=12345

enter image description here

参考: 获取组织结构图 - API - Microsoft 问答,作者:CarlZhao-MSFT

© www.soinside.com 2019 - 2024. All rights reserved.