[英]How to interact with Azure AD B2C custom User Attributes via Microsoft Graph PowerShell SDK?
我通过以下方式添加了一个名为Company Name
的自定义用户属性:
Azure AD B2C
> User attributes
以便在成功登录后可以在身份令牌中返回此信息。
我想为 Azure AD B2C 租户中的每个用户更新这些值。
据我了解:
我无法通过 Azure 门户更新这些值
更新这些值的唯一方法是通过 Microsoft Graph ,特别是这些方法:
我不想为了能够执行这个基本的管理任务而必须创建一个应用程序。
所以我在看:
我在PowerShell 7安装了Microsoft Graph PowerShell SDK 。
运行此命令后,系统提示我通过浏览器登录:
Connect-MgGraph -Scopes "User.Read.All","Group.ReadWrite.All"
在这一点上,我对使用哪些凭据登录感到困惑。
我使用我的“家庭租户”Azure AD 凭据登录。
(即我从中创建 Azure AD B2C 租户的 Azure AD 租户的管理员凭据 - 然后自动在 B2C 租户中创建一个来宾帐户,用户主体名称为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 租户。
所以我的问题是:
在 PowerShell 7 中,安装了Microsoft Graph PowerShell SDK
,如何登录才能与 Azure AD B2C 租户用户而不是我的“主”目录租户用户进行交互。
编辑:
我开始尝试按照此处描述的过程进行操作:
通过 Microsoft Graph 使用仅应用程序身份验证 PowerShell SDK
第一步是:
您需要在运行脚本的计算机上的用户受信任存储区中安装 X.509 证书
我创建了一个应用程序注册,但是在Certificates & secrets
部分它说:
请注意,证书不能用于针对 Azure AD B2C 进行身份验证。
我同意这很棘手。 以下是您可以使用 Microsoft Graph SDK 成功登录到 Azure AD B2C 并更新用户的自定义属性值的步骤。
这篇文章分为两部分:
这篇文章假设我们在 Azure AD B2C 中定义了一个名为Company Name
的自定义属性:
PowerShell Microsoft Graph SDK 参考
为了定位自己,这里是Microsoft.Graph.Users
部分的链接:
变量
以下是本文中将引用的变量以及在哪里可以找到它们。
您可能希望在流程开始时抓住它们,以便以后轻松引用它们。
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
值 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
将如下所示:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.