I am trying to create a new user in my tenant using Microsoft Graph (v1.0) with help of the Microsoft doc.
When I create my user, I always get an error 400 bad request
as response.
I am using HttpClient to make the post Request.
My Function :
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();
}
My token is valid and i am able to make simple Get requests, my app have the authorizations mentioned here.
For example, my json var can contains :
{
"accountEnabled":true,
"displayName":"cyril testgraphh",
"mailNickName":"cyriltestgraphh",
"userPrincipalName":"[email protected]",
"passwordProfile":{
"forceChangePasswordNextSignIn":true,
"password":"XXX"
}
}
EDIT : I solved my problem by using Microsoft Graph objects (Microsoft.Graph.User and Microsoft.Graph.PasswordProfile) and add .onmicrosoft.com to my upn
https://graph.microsoft.com/v1.0/users/
orhttps://graph.microsoft.com/v1.0/users?$filter=startswith(displayname,'someName')
– Challouatte Cyril