我有一个已注册的应用程序,并创建了 Secrete。
我可以使用上面的客户端 ID、租户 ID 和秘密值获取访问令牌。
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "grant_type=client_credentials&client_id=xxx&client_secret=xxx&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default" https://login.microsoftonline.com/xxx/oauth2/v2.0/token
权限已设置。
我可以毫无问题地上传自定义政策。
az rest --method put --headers "Content-Type=application/xml" "Authorization=Bearer xxxxx " --url 'https://graph.microsoft.com/beta/trustFramework/policies/xxx/$value' --body "@xxx.xml"
现在我更改为使用服务主体登录来获取令牌,使用与上面相同的客户端 ID、租户 ID 和秘密值。
az login --service-principal --username $client_id --password $client_secret --tenant $tenant_id
# access_token=$(az account get-access-token --resource-type ms-graph --query accessToken -o tsv | tr -d '\r')
我可以取回访问令牌。但是当我尝试上传文件时,出现此错误。
Forbidden({"error":{"code":"AADB2C","message":"'3b3bf757-6233-4fd2-9ed0-df708b80d59d' is not an Azure AD B2C directory. Access to this Api can only be made for an Azure AD B2C directory.",
我在使用服务主体时是否正确登录?为什么无法上传?
如果您使用正常 Azure AD 租户服务主体获取令牌并将自定义策略上传到 B2C 租户,则会发生错误。
最初,当我通过传递正常的 Azure AD 服务主体凭据和租户 ID 运行下面的脚本时,也遇到了相同的错误。
tenant_id="AADtenantId"
client_id="AADappID"
client_secret="AADsecret"
policy_file="C:\custompolicy\B2C_1A_ChangePhoneNumber.xml"
az login --service-principal --username $client_id --password $client_secret --tenant $tenant_id --allow-no-subscriptions
access_token=$(az account get-access-token --resource-type ms-graph --query accessToken -o tsv | tr -d '\r')
az rest --method put \
--uri "https://graph.microsoft.com/beta/trustFramework/policies/B2C_1A_ChangePhoneNumber/\$value" \
--headers "Content-Type=application/xml" "Authorization=Bearer $access_token" \
--body "@$policy_file"
回复:
要解决该错误,请确保传递 Azure AD B2C 服务主体凭据和租户 ID。
您还可以将您的 B2C 租户名称 作为
--tenant
的值传递。就我而言,我通过传递 Azure AD B2C 租户名称来运行下面的脚本,并得到像这样的响应:
tenant_name="b2ctenant.onmicrosoft.com"
#tenant_id="AADB2C_tenantID"
client_id="AADB2C_appId"
client_secret="AADB2C_secret"
policy_file="C:\custompolicy\B2C_1A_ChangePhoneNumber.xml"
az login --service-principal --username $client_id --password $client_secret --tenant $tenant_name --allow-no-subscriptions
access_token=$(az account get-access-token --resource-type ms-graph --query accessToken -o tsv | tr -d '\r')
az rest --method put \
--uri "https://graph.microsoft.com/beta/trustFramework/policies/B2C_1A_ChangePhoneNumber/\$value" \
--headers "Content-Type=application/xml" "Authorization=Bearer $access_token" \
--body "@$policy_file"
回复:
为了确认,我在门户中检查了相同的内容,其中自定义策略已成功上传,如下所示:
根据您的情况,请交叉检查您是否在第二个请求中传递 Azure AD B2C 服务主体凭据和租户 ID。否则,按照我上面的建议,将您的 B2C 租户名称 作为
--tenant
的值传递。