错误 400 - 使用 Microsoft Graph 错误请求创建用户
Error 400 - Bad Request an Creating user with Microsoft Graph
我正在尝试在 Microsoft 文档的帮助下使用 Microsoft Graph (v1.0) 在我的租户中创建一个新用户。
当我创建我的用户时,我总是得到一个 error 400 bad request
作为响应。
我正在使用 HttpClient 发出 post 请求。
我的函数:
private async Task<string> BuildUser(string token, string query)
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
UserCreation uc = new UserCreation
{
accountEnabled = this.checkBoxActive.Checked,
displayName = this.textBoxDN.Text,
mailNickName = this.textBoxMail.Text,
passwordProfile = new PasswordProfile { forceChangePasswordNextSignIn = this.checkBoxChangeMDP.Checked, password = this.textBoxPassword.Text},
userPrincipalName = this.textBoxUPN.Text
};
string json = JsonConvert.SerializeObject(uc);
var content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = httpClient.PostAsync(query, content).Result;
return response.ToString();
}
我的令牌有效,我可以发出简单的 Get 请求,我的应用程序具有提到的授权 here。
例如,我的 json 变量可以包含:
{
"accountEnabled":true,
"displayName":"cyril testgraphh",
"mailNickName":"cyriltestgraphh",
"userPrincipalName":"cyriltestgraphh@mytenant.fr",
"passwordProfile":{
"forceChangePasswordNextSignIn":true,
"password":"XXX"
}
}
编辑:
我通过使用 Microsoft Graph 对象(Microsoft.Graph.User 和 Microsoft.Graph.PasswordProfile)解决了我的问题,并将 .onmicrosoft.com 添加到我的 upn
你应该检查你的代码。我试过下面的代码,效果很好。
using Microsoft.Graph;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
UserCreation uc = new UserCreation
{
accountEnabled = true,
displayName = "cyril testgraphh",
mailNickName = "cyriltestgraphh",
passwordProfile = new PasswordProfile { ForceChangePasswordNextSignIn = false, Password = "Password!" },
userPrincipalName = "XXXXXX@jmaster.onmicrosoft.com"
};
string json = JsonConvert.SerializeObject(uc);
var content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = httpClient.PostAsync("https://graph.microsoft.com/v1.0/users", content).Result;
Console.Write(response);
Console.ReadLine();
}
}
class UserCreation
{
public bool accountEnabled { get; internal set; }
public string displayName { get; internal set; }
public string mailNickName { get; internal set; }
public string userPrincipalName { get; internal set; }
public PasswordProfile passwordProfile { get; internal set; }
}
}
响应如下:
在我的例子中,从 Azure Graph API 返回的唯一内容是错误 400 - 错误请求...没有别的。 :(
我做了什么来解决它?我正在使用完整的 Group object from Microsoft.Graph NuGet 包在 Azure B2C 上创建一个组。但是,我只是使用了该对象的 5 个属性。在序列化到 JSON 时,它正在序列化 class 的所有属性,并且出于某种原因,Graph API 正在抱怨格式错误的请求。
所以我刚刚创建了一个 anonymous type 具有我需要的属性:
var group = new
{
DisplayName = theGroupName,
Description = $"User group for {theGroupName}",
MailNickname = theGroupName.Replace(" ", string.Empty),
MailEnabled = false,
SecurityEnabled = true
};
将这个简化的对象发送到 Graph API 端点后,我得到了成功响应。
我正在尝试在 Microsoft 文档的帮助下使用 Microsoft Graph (v1.0) 在我的租户中创建一个新用户。
当我创建我的用户时,我总是得到一个 error 400 bad request
作为响应。
我正在使用 HttpClient 发出 post 请求。
我的函数:
private async Task<string> BuildUser(string token, string query)
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
UserCreation uc = new UserCreation
{
accountEnabled = this.checkBoxActive.Checked,
displayName = this.textBoxDN.Text,
mailNickName = this.textBoxMail.Text,
passwordProfile = new PasswordProfile { forceChangePasswordNextSignIn = this.checkBoxChangeMDP.Checked, password = this.textBoxPassword.Text},
userPrincipalName = this.textBoxUPN.Text
};
string json = JsonConvert.SerializeObject(uc);
var content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = httpClient.PostAsync(query, content).Result;
return response.ToString();
}
我的令牌有效,我可以发出简单的 Get 请求,我的应用程序具有提到的授权 here。
例如,我的 json 变量可以包含:
{
"accountEnabled":true,
"displayName":"cyril testgraphh",
"mailNickName":"cyriltestgraphh",
"userPrincipalName":"cyriltestgraphh@mytenant.fr",
"passwordProfile":{
"forceChangePasswordNextSignIn":true,
"password":"XXX"
}
}
编辑: 我通过使用 Microsoft Graph 对象(Microsoft.Graph.User 和 Microsoft.Graph.PasswordProfile)解决了我的问题,并将 .onmicrosoft.com 添加到我的 upn
你应该检查你的代码。我试过下面的代码,效果很好。
using Microsoft.Graph;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
UserCreation uc = new UserCreation
{
accountEnabled = true,
displayName = "cyril testgraphh",
mailNickName = "cyriltestgraphh",
passwordProfile = new PasswordProfile { ForceChangePasswordNextSignIn = false, Password = "Password!" },
userPrincipalName = "XXXXXX@jmaster.onmicrosoft.com"
};
string json = JsonConvert.SerializeObject(uc);
var content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = httpClient.PostAsync("https://graph.microsoft.com/v1.0/users", content).Result;
Console.Write(response);
Console.ReadLine();
}
}
class UserCreation
{
public bool accountEnabled { get; internal set; }
public string displayName { get; internal set; }
public string mailNickName { get; internal set; }
public string userPrincipalName { get; internal set; }
public PasswordProfile passwordProfile { get; internal set; }
}
}
响应如下:
在我的例子中,从 Azure Graph API 返回的唯一内容是错误 400 - 错误请求...没有别的。 :(
我做了什么来解决它?我正在使用完整的 Group object from Microsoft.Graph NuGet 包在 Azure B2C 上创建一个组。但是,我只是使用了该对象的 5 个属性。在序列化到 JSON 时,它正在序列化 class 的所有属性,并且出于某种原因,Graph API 正在抱怨格式错误的请求。
所以我刚刚创建了一个 anonymous type 具有我需要的属性:
var group = new
{
DisplayName = theGroupName,
Description = $"User group for {theGroupName}",
MailNickname = theGroupName.Replace(" ", string.Empty),
MailEnabled = false,
SecurityEnabled = true
};
将这个简化的对象发送到 Graph API 端点后,我得到了成功响应。