以编程方式创建 Azure AD B2C(预览版)用户
Create an Azure AD B2C (Preview) User Programmatically
我最近尝试以编程方式在我的 Azure AD B2C(预览版)实例中创建一个用户。阻碍我的部分只是试图取回令牌。到目前为止我已经尝试过:
var clientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
var clientSecret = @"xxxxxxxxxxxxxxxx";
var tenant = "xxxxxxxxxxxx.onmicrosoft.com";
var authContext = new AuthenticationContext("https://login.microsoftonline.com/" + tenant);
var credential = new ClientCredential(clientId, clientSecret);
var result = await authContext.AcquireTokenAsync("https://graph.windows.net", credential);
当执行到最后一行尝试获取令牌时,我得到:
Microsoft.IdentityModel.Clients.ActiveDirectory.AdalServiceException: AADSTS70001: Application 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' is not supported for this API version.
我可以从这个错误中推断出什么?这是否意味着已暂时删除 Azure AD B2C 的图形访问权限?或者我需要启用某些东西吗?还是我的端点 URL 之一有误?
为了连接到 Graph API,您需要设置一个单独的 clientId 和密码。按照此 B2C 教程创建服务主体并附加 3 Graph API 权限:
一定要使用 ADAL v2(或刚刚发布的 ADAL v3),而不是 ADAL v4,它只是针对 B2C 的实验。
将来 MSAL 库有望结合所有这些。
以下脚本允许您在 1 个 powershell 脚本中创建它:
$msolcred = Get-Credential
Connect-MsolService -credential $msolcred
$bytes = New-Object Byte[] 32
$rand = [System.Security.Cryptography.RandomNumberGenerator]::Create()
$rand.GetBytes($bytes)
$rand.Dispose()
$newClientSecret = [System.Convert]::ToBase64String($bytes)
$principal = New-MsolServicePrincipal -DisplayName "Dummy" -Type password -Value $newClientSecret
Add-MsolRoleMember -RoleObjectId 88d8e3e3-8f55-4a1e-953a-9b9898b8876b -RoleMemberObjectId $principal.ObjectId -RoleMemberType servicePrincipal
Add-MsolRoleMember -RoleObjectId 9360feb5-f418-4baa-8175-e2a00bac4301 -RoleMemberObjectId $principal.ObjectId -RoleMemberType servicePrincipal
Add-MsolRoleMember -RoleObjectId fe930be7-5e62-47db-91af-98c3a49a38b1 -RoleMemberObjectId $principal.ObjectId -RoleMemberType servicePrincipal
Write-Host "clientsecret = $newClientSecret"
Write-Host "clientId = $(($principal).AppPrincipalId)"
Write-Host "tenant = $((Get-MsolDomain).Name)"
我最近尝试以编程方式在我的 Azure AD B2C(预览版)实例中创建一个用户。阻碍我的部分只是试图取回令牌。到目前为止我已经尝试过:
var clientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
var clientSecret = @"xxxxxxxxxxxxxxxx";
var tenant = "xxxxxxxxxxxx.onmicrosoft.com";
var authContext = new AuthenticationContext("https://login.microsoftonline.com/" + tenant);
var credential = new ClientCredential(clientId, clientSecret);
var result = await authContext.AcquireTokenAsync("https://graph.windows.net", credential);
当执行到最后一行尝试获取令牌时,我得到:
Microsoft.IdentityModel.Clients.ActiveDirectory.AdalServiceException: AADSTS70001: Application 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' is not supported for this API version.
我可以从这个错误中推断出什么?这是否意味着已暂时删除 Azure AD B2C 的图形访问权限?或者我需要启用某些东西吗?还是我的端点 URL 之一有误?
为了连接到 Graph API,您需要设置一个单独的 clientId 和密码。按照此 B2C 教程创建服务主体并附加 3 Graph API 权限:
一定要使用 ADAL v2(或刚刚发布的 ADAL v3),而不是 ADAL v4,它只是针对 B2C 的实验。
将来 MSAL 库有望结合所有这些。
以下脚本允许您在 1 个 powershell 脚本中创建它:
$msolcred = Get-Credential
Connect-MsolService -credential $msolcred
$bytes = New-Object Byte[] 32
$rand = [System.Security.Cryptography.RandomNumberGenerator]::Create()
$rand.GetBytes($bytes)
$rand.Dispose()
$newClientSecret = [System.Convert]::ToBase64String($bytes)
$principal = New-MsolServicePrincipal -DisplayName "Dummy" -Type password -Value $newClientSecret
Add-MsolRoleMember -RoleObjectId 88d8e3e3-8f55-4a1e-953a-9b9898b8876b -RoleMemberObjectId $principal.ObjectId -RoleMemberType servicePrincipal
Add-MsolRoleMember -RoleObjectId 9360feb5-f418-4baa-8175-e2a00bac4301 -RoleMemberObjectId $principal.ObjectId -RoleMemberType servicePrincipal
Add-MsolRoleMember -RoleObjectId fe930be7-5e62-47db-91af-98c3a49a38b1 -RoleMemberObjectId $principal.ObjectId -RoleMemberType servicePrincipal
Write-Host "clientsecret = $newClientSecret"
Write-Host "clientId = $(($principal).AppPrincipalId)"
Write-Host "tenant = $((Get-MsolDomain).Name)"