Reputation: 127
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
Upvotes: 3
Views: 2143
Reputation: 102368
In my case the only thing returned from Azure Graph API was the error 400 - Bad Request... nothing else. :(
What I did to solve it? I was using the full blown Group object from Microsoft.Graph NuGet package to create a Group on Azure B2C. However, I was just using 5 properties of that object. While serializing to JSON it was serializing all properties of that class and for some reason the Graph API was barking about a malformed request.
So I just created an anonymous type with the properties that I needed:
var group = new
{
DisplayName = theGroupName,
Description = $"User group for {theGroupName}",
MailNickname = theGroupName.Replace(" ", string.Empty),
MailEnabled = false,
SecurityEnabled = true
};
After sending this streamlined object to the Graph API endpoint, I got a success response.
Upvotes: 1
Reputation: 1925
You should check your code. I tried the following code, it works well.
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 = "[email protected]"
};
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; }
}
}
And the response like this:
Upvotes: 4