错误400 - 错误请求使用Microsoft Graph创建用户

问题描述 投票:1回答:1

我正在尝试使用Microsoft Graph(v1.0)在Microsoft doc的帮助下在我的租户中创建一个新用户。 当我创建我的用户时,我总是得到一个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();
    }

我的令牌是有效的,我能够做出简单的获取请求,我的应用程序有授权here

例如,我的json var可以包含:

{
    "accountEnabled":true,
    "displayName":"cyril testgraphh",
    "mailNickName":"cyriltestgraphh",
    "userPrincipalName":"[email protected]",
    "passwordProfile":{
        "forceChangePasswordNextSignIn":true,
        "password":"XXX"
    }
}

编辑:我通过使用Microsoft Graph对象(Microsoft.Graph.User和Microsoft.Graph.PasswordProfile)解决了我的问题,并将.onmicrosoft.com添加到我的upn

c# .net microsoft-graph azure-ad-graph-api
1个回答
3
投票

你应该检查你的代码。我尝试了以下代码,效果很好。

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

响应是这样的:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.