C# 错误400-使用Microsoft Graph创建用户时请求错误
我正试图在Microsoft文档的帮助下,使用Microsoft Graph(v1.0)在我的租户中创建一个新用户。C# 错误400-使用Microsoft Graph创建用户时请求错误,c#,.net,microsoft-graph-api,azure-ad-graph-api,C#,.net,Microsoft Graph Api,Azure Ad Graph Api,我正试图在Microsoft文档的帮助下,使用Microsoft Graph(v1.0)在我的租户中创建一个新用户。 当我创建我的用户时,我总是得到一个错误400错误请求作为响应 我正在使用HttpClient发出post请求 我的职能: private async Task<string> BuildUser(string token, string query) { httpClient.DefaultRequestHeaders.Authorizatio
当我创建我的用户时,我总是得到一个
错误400错误请求作为响应
我正在使用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();
}
编辑:
我使用Microsoft Graph对象(Microsoft.Graph.User和Microsoft.Graph.PasswordProfile)解决了我的问题,并将.onmicrosoft.com添加到我的upn中。您应该检查代码。我尝试了以下代码,效果很好
使用Microsoft.Graph;
使用Newtonsoft.Json;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Net.Http;
使用System.Net.Http.Header;
使用系统文本;
使用System.Threading.Tasks;
名称空间控制台EAPP4
{
班级计划
{
静态void Main(字符串[]参数)
{
HttpClient HttpClient=新HttpClient();
httpClient.DefaultRequestHeaders.Authorization=新的AuthenticationHeaderValue(“承载者”,令牌);
UserCreation uc=新用户创建
{
accountEnabled=true,
displayName=“cyril testgraphh”,
mailNickName=“西里尔测试图”,
passwordProfile=new passwordProfile{ForceChangePasswordNextSignIn=false,Password=“Password!”},
userPrincipalName=”XXXXXX@jmaster.onmicrosoft.com"
};
字符串json=JsonConvert.SerializeObject(uc);
var content=newstringcontent(json,Encoding.UTF8,“application/json”);
HttpResponseMessage响应=httpClient.PostAsync(“https://graph.microsoft.com/v1.0/users“,内容)。结果;
控制台。写入(响应);
Console.ReadLine();
}
}
类用户创建
{
public bool accountEnabled{get;internal set;}
公共字符串displayName{get;internal set;}
公共字符串mail昵称{get;内部集;}
公共字符串userPrincipalName{get;internal set;}
公共密码配置文件密码配置文件{get;内部集;}
}
}
您是否能够执行其他操作,还是仅此操作失败?是的,我能够发出简单的Get请求,如https://graph.microsoft.com/v1.0/users/
或https://graph.microsoft.com/v1.0/users?$filter=startswith(displayname,'someName')
错误的请求表示某些内容的格式不正确(),是否可以尝试使用“GetAsync(uri)”方法而不是PostAsync()?如下所示:var response=await-httpclient.GetAsync(Uri);您的Json格式和内容正常,所以不是这样。您请求了什么范围?谢谢您的解决方案。我仍然得到400,所以问题一定在别处,我将进一步调查
{
"accountEnabled":true,
"displayName":"cyril testgraphh",
"mailNickName":"cyriltestgraphh",
"userPrincipalName":"cyriltestgraphh@mytenant.fr",
"passwordProfile":{
"forceChangePasswordNextSignIn":true,
"password":"XXX"
}
}