1
votes

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

1
Are you able to perform other actions or is it just this action that fails?Dimitri
Yes, I am able to make simple Get Requests like https://graph.microsoft.com/v1.0/users/ or https://graph.microsoft.com/v1.0/users?$filter=startswith(displayname,'someName')Challouatte Cyril
Bad Request means something is not in the right expected format (docs.microsoft.com/en-us/graph/errors), can you try using the 'GetAsync(uri)' method instead of the PostAsync()? Something like this: var response = await httpclient.GetAsync(Uri); Your Json format and content is OK so its not that.Dimitri
What scopes have you requested?Marc LaFleur

1 Answers

3
votes

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:

enter image description here