gpt4 book ai didi

azure - 在 Azure AD B2C 中使用 Azure AD Graph API 创建用户

转载 作者:行者123 更新时间:2023-12-03 02:59:13 25 4
gpt4 key购买 nike

我正在尝试在 Azure AD B2C 目录中创建用户。我已通过在 cmd 中使用以下命令发布示例 JSON 文件成功创建了用户:

>B2C Create-User <Path to my JSON file>

我想做的是发布一些 JSON 字符串而不是 JSON 文件,命令行给出以下错误:

Error Calling the Graph API:
{
"odata.error": {
"code": "Request_BadRequest",
"message": {
"lang": "en",
"value": "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."
},
"requestId": "965dba69-4488-4d24-88a4-989a6d06cd8d",
"date": "2019-03-19T12:55:27"
}
}

这就是我尝试发布 JSON 的方式:

private static void CreateUser(string[] args)
{

string json = new JavaScriptSerializer().Serialize(new
{
accountEnabled = true,
SignInName = new List<UserInformation.SignInName>()
{
new UserInformation.SignInName(){ type = "emailAddress", value = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="582b393e39363537353136616a183f35393134763b3735" rel="noreferrer noopener nofollow">[email protected]</a>" }
},
creationType = "LocalAccount",
displayName = "Safan Momin",
mailNickname = "Safan",
passwordProfile = new UserInformation.PasswordProfile
{
password = "Mazik@123", forceChangePasswordNextLogin = false
},
passwordPolicies = "DisablePasswordExpiration"

});

object formatted = JsonConvert.DeserializeObject(client.CreateUser(json).Result);
}

这是我的 CreateUser 方法:

 public async Task<string> CreateUser(string json)
{
return await SendGraphPostRequest("/users", json);
}

这是我的 SendGraphPostRequest 方法:

private async Task<string> SendGraphPostRequest(string api, string json)
{
// NOTE: This client uses ADAL v2, not ADAL v4
AuthenticationResult result = authContext.AcquireToken(Globals.aadGraphResourceId, credential);
HttpClient http = new HttpClient();
string url = Globals.aadGraphEndpoint + tenant + api + "?" + Globals.aadGraphVersion;

Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("POST " + url);
Console.WriteLine("Authorization: Bearer " + result.AccessToken.Substring(0, 80) + "...");
Console.WriteLine("Content-Type: application/json");
Console.WriteLine("");
Console.WriteLine("JSON: "+json);
Console.WriteLine("");

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = await http.SendAsync(request);

if (!response.IsSuccessStatusCode)
{
string error = await response.Content.ReadAsStringAsync();
object formatted = JsonConvert.DeserializeObject(error);
throw new WebException("Error Calling the Graph API: \n" + JsonConvert.SerializeObject(formatted, Formatting.Indented));
}

Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine((int)response.StatusCode + ": " + response.ReasonPhrase);
Console.WriteLine("");

return await response.Content.ReadAsStringAsync();
}

最佳答案

我在比较 JSON 字符串时发现了一个错误。我写的是 SignInName 而不是 signInNames

关于azure - 在 Azure AD B2C 中使用 Azure AD Graph API 创建用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55241791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com