使用 Azure AD B2C GraphClient 针对 Javascript/Typescript 中的用户设置附加数据
Use Azure AD B2C GraphClient to set AdditionalData against User in Javascript/Typescript
很像提问者 我有一个管理 Azure AD B2C 用户的应用程序。我正在尝试做与添加具有自定义属性的用户的那个人相同的事情。不同之处在于我试图在 Javascript/Typescript 中执行此操作,但我没有任何运气。我正在努力转换的代码是 Dictionary 对象:
AdditionalData = new Dictionary<string, object>
{
{"OtherEmail", externalUser.Email},
}
我在没有 'AdditionalData' 属性的情况下创建其他用户没有问题,但是当我将该属性添加到我尝试执行的代码时,出现以下错误:
{"message": "A value without a type name was found and no expected type is available. When the model is specified, each value in the payload must have a type which can be either specified in the payload, explicitly by the caller or implicitly inferred from the parent value."}
这是我创建用户的代码:
const batchIDAttributeName = CreateCustomAttribute("AttName"); //extension_<b2c extension client id>_AttName
const user = {
givenName: 'John',
surname: 'Doe',
displayName: 'John Doe',
identities: [
{
signInType: "userName",
issuer: <my tenant>,
issuerAssignedId: 'JDoe',
},
],
passwordProfile: {
password: 'password',
forceChangePasswordNextSignIn: false,
},
additionalData: {
[batchIDAttributeName]: 201522154
},
passwordPolicies: "DisablePasswordExpiration,DisableStrongPassword",
accountEnabled: true,
mail: "",
};
var result = await client.api("/users").post(user);
我也试过:
additionalData: {
key: [batchIDAttributeName],
value: 201522154
}
感谢任何帮助。我相信我不会那么远。
创建具有自定义属性的用户时不需要添加additionalData
。
HTTP:
POST https://graph.microsoft.com/v1.0/users
{
"accountEnabled": true,
"displayName": "Adele Vance",
"mailNickname": "AdeleV",
"userPrincipalName": "AdeleV@<b2c-tenant-name>.onmicrosoft.com",
"passwordProfile": {
"forceChangePasswordNextSignIn": true,
"password": "xWwvJ]6NMw+bWH-d"
},
"extension_<b2c-extensions-app client id without the dashes '-'>_AttName": "123456"
}
JavaScript 与 the MS Graph sample:
// create Users from Graph
async function createUsers(): Promise<any> {
const client = createAuthenticatedClient();
const user = {
accountEnabled: true,
displayName: "Adele Vance3",
mailNickname: "AdeleV3",
userPrincipalName: "AdeleV3@<b2c-tenant-name>.onmicrosoft.com",
"passwordProfile" : {
forceChangePasswordNextSignIn: true,
password: "xWwvJ]6NMw+bWH-d"
},
"extension_xxxxxxxxxxxxxxxxxx_AttName": "123456"
};
const request = await client.api("/users")
.post(user)
.catch((error) => {
console.log(error);
});
console.log(request.value);
}
createUsers();
很像提问者
AdditionalData = new Dictionary<string, object>
{
{"OtherEmail", externalUser.Email},
}
我在没有 'AdditionalData' 属性的情况下创建其他用户没有问题,但是当我将该属性添加到我尝试执行的代码时,出现以下错误:
{"message": "A value without a type name was found and no expected type is available. When the model is specified, each value in the payload must have a type which can be either specified in the payload, explicitly by the caller or implicitly inferred from the parent value."}
这是我创建用户的代码:
const batchIDAttributeName = CreateCustomAttribute("AttName"); //extension_<b2c extension client id>_AttName
const user = {
givenName: 'John',
surname: 'Doe',
displayName: 'John Doe',
identities: [
{
signInType: "userName",
issuer: <my tenant>,
issuerAssignedId: 'JDoe',
},
],
passwordProfile: {
password: 'password',
forceChangePasswordNextSignIn: false,
},
additionalData: {
[batchIDAttributeName]: 201522154
},
passwordPolicies: "DisablePasswordExpiration,DisableStrongPassword",
accountEnabled: true,
mail: "",
};
var result = await client.api("/users").post(user);
我也试过:
additionalData: {
key: [batchIDAttributeName],
value: 201522154
}
感谢任何帮助。我相信我不会那么远。
创建具有自定义属性的用户时不需要添加additionalData
。
HTTP:
POST https://graph.microsoft.com/v1.0/users
{
"accountEnabled": true,
"displayName": "Adele Vance",
"mailNickname": "AdeleV",
"userPrincipalName": "AdeleV@<b2c-tenant-name>.onmicrosoft.com",
"passwordProfile": {
"forceChangePasswordNextSignIn": true,
"password": "xWwvJ]6NMw+bWH-d"
},
"extension_<b2c-extensions-app client id without the dashes '-'>_AttName": "123456"
}
JavaScript 与 the MS Graph sample:
// create Users from Graph
async function createUsers(): Promise<any> {
const client = createAuthenticatedClient();
const user = {
accountEnabled: true,
displayName: "Adele Vance3",
mailNickname: "AdeleV3",
userPrincipalName: "AdeleV3@<b2c-tenant-name>.onmicrosoft.com",
"passwordProfile" : {
forceChangePasswordNextSignIn: true,
password: "xWwvJ]6NMw+bWH-d"
},
"extension_xxxxxxxxxxxxxxxxxx_AttName": "123456"
};
const request = await client.api("/users")
.post(user)
.catch((error) => {
console.log(error);
});
console.log(request.value);
}
createUsers();