【问题标题】:Issue when create a user on Azure B2C using Microsoft Graph API使用 Microsoft Graph API 在 Azure B2C 上创建用户时出现问题
【发布时间】:2021-11-15 16:24:03
【问题描述】:

问题

我正在尝试构建一个小脚本 PHP 以与 Microsoft Graph API 集成。

我在 Azure 中有一个管理员帐户,并通过门户设置了一个新应用程序。我已经下载并安装了PHP SDK,并设法设置了所有内容,以便成功获取用户。

但是,发布一个新用户会返回一个 400 错误请求:

$newUser = new User();
$newUser->setAccountEnabled(true);
$newUser->setGivenName('name');
$newUser->setSurname('surname');
$newUser->setUserPrincipalName('email@dot.com');

$password = new PasswordProfile();
$password->setPassword("pwd");
$password->setForceChangePasswordNextSignIn(false);
$newUser->setPasswordProfile($password);

$identities = new ObjectIdentity();
$identities->setSignInType("emailAddress");
$identities->setIssuer("a tenantId");
$identities->setIssuerAssignedId("email@dot.com");
$newUser->setIdentities($identities);

$user = $this->graph->createRequest('POST', '/users')
        ->attachBody($newUser)
        ->setReturnType(Model\User::class)
        ->execute();

返回:

{"error":{"code":"BadRequest","message":"Property identities in payload has a value that does not match schema.","innerError":{"date":"2021-11-15T15:26:24","request-id":"an id","client-request-id":"an id"}}}

我该如何解决这个问题?

感谢您的帮助。

更新

我已经找到了解决这个问题的方法(显然是暂时的)。

“身份”部分必须设置为数组(在 JSON 中),如本例所示

"identities": [
{
  "signInType": "emailAddress",
  "issuer": "contoso.onmicrosoft.com",
  "issuerAssignedId": "jsmith@yahoo.com"
}
]

可能上面的 PHP 代码没有将“身份”部分设置为数组。

现在,我直接发送带有“身份”部分的 JSON 作为数组,但我想使用上面的 PHP 代码。

如何转换上面的 PHP 代码,以像 JSON 示例中那样将“身份”作为数组传递?

再次感谢。

【问题讨论】:

  • 你可以试试$identities->setIssuer("tenant domain name");而不是租户 ID
  • 你好@RamaraoAdapa-MT。感谢您的答复。我找到了解决方案。 “身份”部分必须被视为 JSON 中的数组。可能,我的代码没有像数组一样设置“身份”部分。
  • 你能试试这段代码将身份设置为数组吗:$identity1 = new ObjectIdentity(); $identity1->setSignInType("emailAddress"); $identity1->setIssuer("a tenantId"); $identity1->setIssuerAssignedId("email@dot.com"); $identities = 数组($identity1); $newUser->setIdentities($identities);
  • 你好@RamaraoAdapa-MT。再次感谢您的回复。我会试试你的建议。谢谢。

标签: php azure-active-directory microsoft-graph-api microsoft-graph-sdks


【解决方案1】:

尝试创建一个数组并在其中添加身份。这会将 identities 属性添加为 Model\ObjectIdentity() 的数组

 ...
 $identities = [];

 $identity = new Model\ObjectIdentity();
 $identity->setSignInType("type");
 $identity->setIssuer("issuer");
 $identity->setIssuerAssignedId("issure-assigned-id");

 array_push($identities, $identity);

 $newUser->setIdentities($identities);
 ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-10
    • 1970-01-01
    • 2021-02-17
    • 2018-03-02
    • 2020-05-10
    • 1970-01-01
    • 2020-07-31
    • 1970-01-01
    相关资源
    最近更新 更多