为用户分配、更新、列出或删除自定义安全属性

属于 Microsoft Entra 的 Microsoft Entra ID 中的自定义安全属性是特定于业务的属性(键值对),你可以定义这些属性并将其分配给 Microsoft Entra 对象。 例如,可分配自定义安全属性以筛选员工或帮助确定访问资源的人员。 本文介绍如何为 Microsoft Entra ID 分配、更新、列出或删除自定义安全属性。

先决条件

若要为 Microsoft Entra 租户中的用户分配或删除自定义安全属性,需要:

重要

默认情况下,全局管理员和其他管理员角色无权读取、定义或分配自定义安全属性。

向用户分配自定义安全属性

提示

本文中的步骤可能因开始使用的门户而略有不同。

  1. 属性分配管理员身份登录到 Microsoft Entra 管理中心

  2. 请确保你已定义自定义安全属性。 有关详细信息,请参阅在 Microsoft Entra ID 中添加或停用自定义安全属性定义

  3. 浏览到“标识”>“用户”>“所有用户”。

  4. 查找并选择要为其分配自定义安全属性的用户。

  5. 在“管理”部分,选择“自定义安全属性”。

  6. 选择“添加分配”。

  7. 在“属性集”中,从列表中选择一个属性集。

  8. 在“属性名称”中,从列表中选择一个自定义安全属性。

  9. 根据所选自定义安全属性的属性,可以输入单个值、从预定义列表中选择一个值或添加多个值。

    • 对于自由格式的单值自定义安全属性,请在“分配的值”框中输入一个值。
    • 对于预定义的自定义安全属性值,请从“分配的值”列表中选择一个值。
    • 对于多值自定义安全属性,请选择“添加值”以打开“属性值”窗格并添加值。 完成添加值后,选择“完成”。

    Screenshot showing assigning a custom security attribute to a user.

  10. 完成后,选择“保存”,将自定义安全属性分配给用户。

更新用户的自定义安全属性分配值

  1. 属性分配管理员身份登录到 Microsoft Entra 管理中心

  2. 浏览到“标识”>“用户”>“所有用户”。

  3. 找到并选择具有要更新的自定义安全属性分配值的用户。

  4. 在“管理”部分,选择“自定义安全属性”。

  5. 找到要更新的自定义安全属性分配值。

    向用户分配自定义安全属性后,只能更改自定义安全属性的值。 不能更改自定义安全属性的其他属性,如属性集或属性名称。

  6. 根据所选自定义安全属性的属性,可以更新单个值、从预定义列表中选择一个值或更新多个值。

  7. 完成后,选择“保存”。

基于自定义安全属性分配筛选用户

可以在“所有用户”页上筛选分配给用户的自定义安全属性列表。

  1. 属性分配读者身份登录到 Microsoft Entra 管理中心

  2. 浏览到“标识”>“用户”>“所有用户”。

  3. 选择“添加筛选器”以打开“添加筛选器”窗格。

  4. 选择“自定义安全属性”。

  5. 选择属性集和属性名称。

  6. 对于“运算符”,可选择等于 (==)、不等于 (!=) 或开头内容。

  7. 对于“值”,请输入或选择一个值。

    Screenshot showing a custom security attribute filter for users.

  8. 选择“应用”以应用筛选器。

从用户中删除自定义安全属性分配

  1. 属性分配管理员身份登录到 Microsoft Entra 管理中心

  2. 浏览到“标识”>“用户”>“所有用户”。

  3. 找到并选择具有要删除的自定义安全属性分配的用户。

  4. 在“管理”部分,选择“自定义安全属性”。

  5. 在要删除的所有自定义安全属性分配旁边添加复选标记。

  6. 选择“删除分配”。

PowerShell 或 Microsoft Graph API

若要为 Microsoft Entra 组织中的用户管理自定义安全属性分配,可使用 PowerShell 或 Microsoft Graph API。 以下示例可用于管理分配。

向用户分配具有字符串值的自定义安全属性

以下示例向用户分配具有字符串值的自定义安全属性。

  • 属性集:Engineering
  • 属性:ProjectDate
  • 属性数据类型:字符串
  • 属性值:"2024-11-15"

Update-MgUser

$customSecurityAttributes = @{
    "Engineering" = @{
        "@odata.type" = "#Microsoft.DirectoryServices.CustomSecurityAttributeValue"
        "ProjectDate" = "2024-11-15"
    }
}
Update-MgUser -UserId $userId -CustomSecurityAttributes $customSecurityAttributes

向用户分配具有多字符串值的自定义安全属性

以下示例向用户分配具有多字符串值的自定义安全属性。

  • 属性集:Engineering
  • 属性:Project
  • 属性数据类型:字符串集合
  • 属性值:["Baker","Cascade"]

Update-MgUser

$customSecurityAttributes = @{
    "Engineering" = @{
        "@odata.type" = "#Microsoft.DirectoryServices.CustomSecurityAttributeValue"
        "Project@odata.type" = "#Collection(String)"
        "Project" = @("Baker","Cascade")
    }
}
Update-MgUser -UserId $userId -CustomSecurityAttributes $customSecurityAttributes

向用户分配具有整数值的自定义安全属性

以下示例向用户分配具有整数值的自定义安全属性。

  • 属性集:Engineering
  • 属性:NumVendors
  • 属性数据类型:整数
  • 属性值:4

Update-MgUser

$customSecurityAttributes = @{
    "Engineering" = @{
        "@odata.type" = "#Microsoft.DirectoryServices.CustomSecurityAttributeValue"
        "NumVendors@odata.type" = "#Int32"
        "NumVendors" = 4
    }
}
Update-MgUser -UserId $userId -CustomSecurityAttributes $customSecurityAttributes

向用户分配具有整数值的自定义安全属性

以下示例向用户分配具有多整数值的自定义安全属性。

  • 属性集:Engineering
  • 属性:CostCenter
  • 属性数据类型:整数集合
  • 属性值:[1001,1003]

Update-MgUser

$customSecurityAttributes = @{
    "Engineering" = @{
        "@odata.type" = "#Microsoft.DirectoryServices.CustomSecurityAttributeValue"
        "CostCenter@odata.type" = "#Collection(Int32)"
        "CostCenter" = @(1001,1003)
    }
}
Update-MgUser -UserId $userId -CustomSecurityAttributes $customSecurityAttributes

向用户分配具有布尔值的自定义安全属性

以下示例向用户分配具有布尔值的自定义安全属性。

  • 属性集:Engineering
  • 属性:Certification
  • 属性数据类型:布尔值
  • 属性值:true

Update-MgUser

$customSecurityAttributes = @{
    "Engineering" = @{
        "@odata.type" = "#Microsoft.DirectoryServices.CustomSecurityAttributeValue"
        "Certification" = $true
    }
}
Update-MgUser -UserId $userId -CustomSecurityAttributes $customSecurityAttributes

为用户更新具有整数值的自定义安全属性分配

以下示例为用户更新具有整数值的自定义安全属性分配。

  • 属性集:Engineering
  • 属性:NumVendors
  • 属性数据类型:整数
  • 属性值:8

Update-MgUser

$customSecurityAttributes = @{
    "Engineering" = @{
        "@odata.type" = "#Microsoft.DirectoryServices.CustomSecurityAttributeValue"
        "NumVendors@odata.type" = "#Int32"
        "NumVendors" = 8
    }
}
Update-MgUser -UserId $userId -CustomSecurityAttributes $customSecurityAttributes

为用户更新具有布尔值的自定义安全属性分配

以下示例为用户更新具有布尔值的自定义安全属性分配。

  • 属性集:Engineering
  • 属性:Certification
  • 属性数据类型:布尔值
  • 属性值:false

Update-MgUser

$customSecurityAttributes = @{
    "Engineering" = @{
        "@odata.type" = "#Microsoft.DirectoryServices.CustomSecurityAttributeValue"
        "Certification" = $false
    }
}
Update-MgUser -UserId $userId -CustomSecurityAttributes $customSecurityAttributes

为用户更新具有多字符串值的自定义安全属性分配

以下示例为用户更新具有多字符串值的自定义安全属性分配。

  • 属性集:Engineering
  • 属性:Project
  • 属性数据类型:字符串集合
  • 属性值:("Alpine","Baker")

Update-MgUser

$customSecurityAttributes = @{
    "Engineering" = @{
        "@odata.type" = "#Microsoft.DirectoryServices.CustomSecurityAttributeValue"
        "Project@odata.type" = "#Collection(String)"
        "Project" = @("Alpine","Baker")
    }
}
Update-MgUser -UserId $userId -CustomSecurityAttributes $customSecurityAttributes

获取用户的自定义安全属性分配信息

以下示例获取用户的自定义安全属性分配。

Get-MgUser

$userAttributes = Get-MgUser -UserId $userId -Property "customSecurityAttributes"
$userAttributes.CustomSecurityAttributes.AdditionalProperties | Format-List
$userAttributes.CustomSecurityAttributes.AdditionalProperties.Engineering
$userAttributes.CustomSecurityAttributes.AdditionalProperties.Marketing
Key   : Engineering
Value : {[@odata.type, #microsoft.graph.customSecurityAttributeValue], [Project@odata.type, #Collection(String)], [Project, System.Object[]],
        [ProjectDate, 2024-11-15]…}

Key   : Marketing
Value : {[@odata.type, #microsoft.graph.customSecurityAttributeValue], [EmployeeId, GS45897]}


Key                   Value
---                   -----
@odata.type           #microsoft.graph.customSecurityAttributeValue
Project@odata.type    #Collection(String)
Project               {Baker, Alpine}
ProjectDate           2024-11-15
NumVendors            8
CostCenter@odata.type #Collection(Int32)
CostCenter            {1001, 1003}
Certification         False


Key         Value
---         -----
@odata.type #microsoft.graph.customSecurityAttributeValue
EmployeeId  KX45897

如果没有为用户分配自定义安全属性,或者如果调用的主体没有访问权限,则响应将为空。

列出自定义安全属性分配等于某个值的所有用户

以下示例列出了自定义安全属性分配等于某个值的所有用户。 它检索自定义安全属性名为 AppCountry 且值等于 Canada 的用户。 筛选器值区分大小写。 必须在请求或标头中添加 ConsistencyLevel=eventual。 还必须包括 $count=true 以确保正确地路由请求。

  • 属性集:Marketing
  • 属性:AppCountry
  • 筛选器:AppCountry eq 'Canada'

Get-MgUser

$userAttributes = Get-MgUser -CountVariable CountVar -Property "id,displayName,customSecurityAttributes" -Filter "customSecurityAttributes/Marketing/AppCountry eq 'Canada'" -ConsistencyLevel eventual
$userAttributes | select Id,DisplayName,CustomSecurityAttributes
$userAttributes.CustomSecurityAttributes.AdditionalProperties | Format-List
Id                                   DisplayName CustomSecurityAttributes
--                                   ----------- ------------------------
4b4e8090-e9ba-4bdc-b2f0-67c3c7c59489 Jiya        Microsoft.Graph.PowerShell.Models.MicrosoftGraphCustomSecurityAttributeValue
efdf3082-64ae-495f-b051-855e2d8df969 Jana        Microsoft.Graph.PowerShell.Models.MicrosoftGraphCustomSecurityAttributeValue

Key   : Engineering
Value : {[@odata.type, #microsoft.graph.customSecurityAttributeValue], [Datacenter@odata.type, #Collection(String)], [Datacenter, System.Object[]]}

Key   : Marketing
Value : {[@odata.type, #microsoft.graph.customSecurityAttributeValue], [AppCountry@odata.type, #Collection(String)], [AppCountry, System.Object[]],
        [EmployeeId, KX19476]}

Key   : Marketing
Value : {[@odata.type, #microsoft.graph.customSecurityAttributeValue], [AppCountry@odata.type, #Collection(String)], [AppCountry, System.Object[]],
        [EmployeeId, GS46982]}

列出自定义安全属性分配以某个值开头的所有用户

以下示例列出了自定义安全属性分配以某个值开头的所有用户。 它检索自定义安全属性名为 EmployeeId 且值以 GS 开头的用户。 筛选器值区分大小写。 必须在请求或标头中添加 ConsistencyLevel=eventual。 还必须包括 $count=true 以确保正确地路由请求。

  • 属性集:Marketing
  • 属性:EmployeeId
  • 筛选器:EmployeeId startsWith 'GS'

Get-MgUser

$userAttributes = Get-MgUser -CountVariable CountVar -Property "id,displayName,customSecurityAttributes" -Filter "startsWith(customSecurityAttributes/Marketing/EmployeeId,'GS')" -ConsistencyLevel eventual
$userAttributes | select Id,DisplayName,CustomSecurityAttributes
$userAttributes.CustomSecurityAttributes.AdditionalProperties | Format-List
Id                                   DisplayName CustomSecurityAttributes
--                                   ----------- ------------------------
02d52406-be75-411b-b02f-29d7f38dcf62 Chandra     Microsoft.Graph.PowerShell.Models.MicrosoftGraphCustomSecurityAttributeValue
efdf3082-64ae-495f-b051-855e2d8df969 Jana        Microsoft.Graph.PowerShell.Models.MicrosoftGraphCustomSecurityAttributeValue
d5a1c025-2d79-4ad3-9217-91ac3a4ed8b8 Joe         Microsoft.Graph.PowerShell.Models.MicrosoftGraphCustomSecurityAttributeValue

Key   : Marketing
Value : {[@odata.type, #microsoft.graph.customSecurityAttributeValue], [EmployeeId, GS36348]}

Key   : Marketing
Value : {[@odata.type, #microsoft.graph.customSecurityAttributeValue], [AppCountry@odata.type, #Collection(String)], [AppCountry, System.Object[]],
        [EmployeeId, GS46982]}

Key   : Engineering
Value : {[@odata.type, #microsoft.graph.customSecurityAttributeValue], [Project@odata.type, #Collection(String)], [Project, System.Object[]],
        [ProjectDate, 2024-11-15]…}

Key   : Marketing
Value : {[@odata.type, #microsoft.graph.customSecurityAttributeValue], [EmployeeId, GS45897]}

列出自定义安全属性分配不等于某个值的所有用户

以下示例列出了自定义安全属性分配不等于某个值的所有用户。 它检索自定义安全属性名为 AppCountry 且值不等于 Canada 的用户。 筛选器值区分大小写。 必须在请求或标头中添加 ConsistencyLevel=eventual。 还必须包括 $count=true 以确保正确地路由请求。

  • 属性集:Marketing
  • 属性:AppCountry
  • 筛选器:AppCountry ne 'Canada'

Get-MgUser

$userAttributes = Get-MgUser -CountVariable CountVar -Property "id,displayName,customSecurityAttributes" -Filter "customSecurityAttributes/Marketing/AppCountry ne 'Canada'" -ConsistencyLevel eventual
$userAttributes | select Id,DisplayName,CustomSecurityAttributes
Id                                   DisplayName              CustomSecurityAttributes
--                                   -----------              ------------------------
02d52406-be75-411b-b02f-29d7f38dcf62 Chandra                  Microsoft.Graph.PowerShell.Models.MicrosoftGraphCustomSecurityAttributeValue
eaea4971-7764-4498-9aeb-776496812e75 Isabella                 Microsoft.Graph.PowerShell.Models.MicrosoftGraphCustomSecurityAttributeValue
d937580c-692c-451f-a507-6758d3bdf353 Alain                    Microsoft.Graph.PowerShell.Models.MicrosoftGraphCustomSecurityAttributeValue
d5a1c025-2d79-4ad3-9217-91ac3a4ed8b8 Joe                      Microsoft.Graph.PowerShell.Models.MicrosoftGraphCustomSecurityAttributeValue
23ad8721-f46c-421a-9785-33b0ef474198 Dara                     Microsoft.Graph.PowerShell.Models.MicrosoftGraphCustomSecurityAttributeValue

删除用户的单值自定义安全属性分配

以下示例通过将值设置为 null,从用户中删除单值自定义安全属性分配。

  • 属性集:Engineering
  • 属性:ProjectDate
  • 属性值:null

Invoke-MgGraphRequest

$params = @{
    "customSecurityAttributes" = @{
        "Engineering" = @{
            "@odata.type" = "#Microsoft.DirectoryServices.CustomSecurityAttributeValue"
            "ProjectDate" = $null
        }
    }
}
Invoke-MgGraphRequest -Method PATCH -Uri "https://microsoftgraph.chinacloudapi.cn/v1.0/users/$userId" -Body $params

删除用户的多值自定义安全属性分配

以下示例通过将值设置为空集合,从用户中删除多值自定义安全属性分配。

  • 属性集:Engineering
  • 属性:Project
  • 属性值:[]

Update-MgUser

$customSecurityAttributes = @{
    "Engineering" = @{
        "@odata.type" = "#Microsoft.DirectoryServices.CustomSecurityAttributeValue"
        "Project" = @()
    }
}
Update-MgUser -UserId $userId -CustomSecurityAttributes $customSecurityAttributes

常见问题

有哪些地方支持用户的自定义安全属性分配?

Microsoft Entra 管理中心、PowerShell 和 Microsoft Graph API 支持用户的自定义安全属性分配。 “我的应用”或 Microsoft 365 管理中心中不支持自定义安全属性分配。

谁可以查看分配给用户的自定义安全属性?

只有已在租户范围内分配到“属性分配管理员”或“属性分配读取者”角色的用户可以查看分配给租户中的任何用户的自定义安全属性。 用户无法查看分配给自己的配置文件或其他用户的自定义安全属性。 无论租户上设置的来宾权限如何,来宾都无法查看自定义安全属性。

是否需要创建一个应用来添加自定义安全属性分配?

不是的,无需应用程序即可将自定义安全属性分配给用户对象。

为什么在尝试保存自定义安全属性分配时一直遇到错误?

你无权向用户分配自定义安全属性。 请确保自己分配到了属性分配管理员角色。

我能否向来宾分配自定义安全属性?

是的,可以将自定义安全属性分配给租户中的成员或来宾。

我能否向目录同步的用户分配自定义安全属性?

可以为本地 Active Directory 中的目录同步用户分配自定义安全属性。

自定义安全属性分配是否可用于动态成员身份规则?

不可以,分配给用户的自定义安全属性不支持配置动态成员身份规则。

自定义安全属性是否与 B2C 租户中的自定义属性相同?

不相同,自定义安全属性在 B2C 租户中不受支持,且与 B2C 功能没有关联。

后续步骤