如何通过 Microsoft Graph PowerShell SDK 与 Azure AD B2C 自定义用户属性交互?

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

我通过以下方式添加了一个名为

Company Name
的自定义用户属性:

Azure AD B2C
>
User attributes

以便登录成功后可以在身份令牌中返回此信息。

我想为 Azure AD B2C 租户中的每个用户更新这些值。

据我了解:

我不想仅仅为了能够执行此基本管理任务而创建应用程序。

所以我正在看:

Microsoft Graph PowerShell SDK

我在 PowerShell 7 中安装了 Microsoft Graph PowerShell SDK

运行此命令后,系统提示我通过浏览器登录:

Connect-MgGraph -Scopes "User.Read.All","Group.ReadWrite.All"

此时我很困惑使用哪些凭据登录。

我使用我的“家庭租户”Azure AD 凭据登录。

(即我创建 Azure AD B2C 租户的 Azure AD 租户的管理员凭据 - 然后自动在 B2C 租户中创建一个 guest 帐户,用户主体名称为

info_my-home-tenant.onmicrosoft.com#EXT#@my-dev-tenant.onmicrosoft.com
)。

我做出这个决定是因为我认为我没有任何 Azure AD B2C 凭据。

(当我使用 Azure AD 凭据登录 Azure 门户时,要访问 Azure AD B2C,只需单击“切换目录”)。

然后我跑了:

Get-MgUser

不出所料,它从我的家庭 Azure AD 租户返回用户,而不是 Azure AD B2C 租户。

所以我的问题是:

在安装了

Microsoft Graph PowerShell SDK
的 PowerShell 7 中,如何登录以便可以与 Azure AD B2C 租户用户交互,而不是与我的“主”目录租户用户交互。

编辑:

我开始尝试遵循此处描述的过程:

通过 Microsoft Graph PowerShell SDK 使用仅应用程序身份验证

第一步是:

您需要在运行脚本的计算机上的用户受信任存储中安装 X.509 证书

我创建了一个应用程序注册,但是在

Certificates & secrets
部分它说:

请注意,证书不能用于针对 Azure AD B2C 进行身份验证。

microsoft-graph-api azure-ad-b2c microsoft-graph-sdks powershell-7.0
1个回答
3
投票

我同意这很棘手。

以下是使用 Microsoft Graph SDK 成功登录 Azure AD B2C 并更新用户的自定义属性值的步骤。

这篇文章分为三个部分:

  • 解决方案摘要(在深入了解细节之前了解范围)
  • 变量(列出了所需的变量值以及在哪里可以找到它们)
  • 命令(列出所需的命令)

本文假设我们在 Azure AD B2C 中定义了一个名为

Company Name
的自定义属性:


PowerShell Microsoft Graph SDK 参考

为了自我定位,这里是

Microsoft.Graph.Users
部分的链接:

Microsoft.Graph.Users


总结

该解决方案需要定义 4 个变量和 5 个引用它们的命令:

变量:

  • azure_ad_b2c_tenant_id
  • extensions_app_id
  • custom_attribute_property
  • user_id

命令:

建立连接:

Connect-MgGraph -TenantId "<azure_ad_b2c_tenant_id>" -Scopes "User.ReadWrite.All"

健全性检查 - 列出所有用户:

Get-MgUser | Format-List  ID, DisplayName, UserPrincipalName

健全性检查 - 查看单个用户自定义属性的现有值:

$existingUser = Get-MgUser -UserId "<user-id>" -Property "id,extension_<your-extensions-app-application-id>_CompanyName"
$existingUser.AdditionalProperties | Format-List

更新用户的自定义属性:

$params = @{extension_<your-extensions-app-application-id>_CompanyName='Test Company'}
Update-MgUser -UserId "<user-id>" -BodyParameter $params

验证更新:

$existingUser = Get-MgUser -UserId "<user-id>" -Property "id,extension_<your-extensions-app-application-id>_CompanyName"
$existingUser.AdditionalProperties | Format-List

变量

下面是将引用的变量以及在哪里可以找到它们。

您可能希望在流程开始时获取它们,以便稍后可以轻松引用它们。

azure_ad_b2c_tenant_id

  • Azure AD B2C directory
    >
    Azure AD
    >
    Tenant ID

extensions_app_id

  • Azure AD B2C
    >
    App registrations
    >
    [ select 'All applications' ]
  • 单击名为:
  • 的项目
  • b2c-extensions-app. Do not modify. Used by AADB2C for storing user data.
  • 复制
    Application (client) ID
  • 在 PowerShell 中使用该值时删除破折号

custom_attribute_property

这是具有以下语法的串联值的字符串:

extension_<your-extensions-app-application-id>_<your-custom-attribute>  

例如:

extension_lalala1234etc_CompanyName

user_id

  • Azure AD B2C
    >
    Users
    >
    [ click on desired user ]
    >
    Object ID


命令

01。连接到您的 Azure AD B2C 租户

Connect-MgGraph -TenantId "<azure_ad_b2c_tenant_id>" -Scopes "User.ReadWrite.All"

这将提示您使用 Azure AD 主租户凭据登录。

02。健全性检查 - 列出所有用户以确认您位于正确的租户中

Get-MgUser   

// you can make the results prettier by using Format-List and defining the columns you want displayed   
Get-MgUser | Format-List  ID, DisplayName, UserPrincipalName

03。健全性检查 - 查看当前所有用户和单个用户的自定义属性值是什么

// all users - these do not work:  
Get-MgUser | Format-List  ID, extension_<your-extensions-app-application-id>_CompanyName
Get-MgUser -Property "id,extension_<your-extensions-app-application-id>_CompanyName"

// single user - these do not work:  
Get-MgUser -UserId "<user-id>" | Format-List  ID, DisplayName, UserPrincipalName, extension_<your-extensions-app-application-id>_CompanyName
Get-MgUser -UserId "<user-id>" -Property "id,extension_<your-extensions-app-application-id>_CompanyName"

// single user - this works:
$existingUser = Get-MgUser -UserId "<user-id>" -Property "id,extension_<your-extensions-app-application-id>_CompanyName"
$existingUser.AdditionalProperties | Format-List

04。更新单个用户的自定义属性

$params = @{extension_<your-extensions-app-application-id>_CompanyName='Test Company'}
Update-MgUser -UserId "<user-id>" -BodyParameter $params

05。验证是否已进行更新

$existingUser = Get-MgUser -UserId "<user-id>" -Property "id,extension_<your-extensions-app-application-id>_CompanyName"
$existingUser.AdditionalProperties | Format-List

登录后返回的解码后的

idToken
将如下所示:

或者,如果通过身份提供商(在本例中为家庭 AD 租户)登录,则解码后的

idToken
将如下所示:

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