Ошибка 400 - Неверный запрос на создание пользователя с помощью Microsoft Graph - PullRequest
0 голосов
/ 12 февраля 2019

Я пытаюсь создать нового пользователя в своем клиенте с помощью Microsoft Graph (v1.0) с помощью документа Microsoft.
Когда я создаю своего пользователя, я всегда получаю error 400 bad request в качестве ответа.

Я использую HttpClient для отправки запроса.

Моя функция:

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, мое приложение имеетупомянутые права доступа здесь .

Например, мой json var может содержать:

{
    "accountEnabled":true,
    "displayName":"cyril testgraphh",
    "mailNickName":"cyriltestgraphh",
    "userPrincipalName":"cyriltestgraphh@mytenant.fr",
    "passwordProfile":{
        "forceChangePasswordNextSignIn":true,
        "password":"XXX"
    }
}

РЕДАКТИРОВАТЬ: я решил свою проблему с помощью объектов Microsoft Graph (Microsoft.Graph.Пользователь и Microsoft.Graph.PasswordProfile) и добавьте .onmicrosoft.com к моему upn

1 Ответ

0 голосов
/ 13 февраля 2019

Вы должны проверить свой код.Я попробовал следующий код, он хорошо работает.

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; }
    }
 
}

И ответ такой:

enter image description here

...