繁体   English   中英

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

[英]Error 400 - Bad Request an Creating user with Microsoft Graph

我正在尝试在 Microsoft 文档的帮助下使用 Microsoft Graph (v1.0) 在我的租户中创建一个新用户。
当我创建我的用户时,我总是收到error 400 bad request作为响应。

我正在使用 HttpClient 发出帖子请求。

我的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();
    }

我的令牌有效,我可以发出简单的 Get 请求,我的应用程序具有此处提到的授权。

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

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

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

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

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

响应是这样的:

在此输入图像描述

在我的例子中,唯一从 Azure Graph API 返回的是错误 400 - Bad Request ... 没有别的。 :(

我做了什么来解决它? 我正在使用来自Microsoft.Graph NuGet package 的完整组 object在 Azure B2C 上创建一个组。 但是,我只是使用了 object 的 5 个属性。在序列化到 JSON 时,它正在序列化 class 的所有属性,并且出于某种原因,图表 API 正在抱怨格式错误的请求。

所以我刚刚创建了一个匿名类型,其中包含我需要的属性:

var group = new
{
    DisplayName = theGroupName,
    Description = $"User group for {theGroupName}",
    MailNickname = theGroupName.Replace(" ", string.Empty),
    MailEnabled = false,
    SecurityEnabled = true
};

将这个简化的 object 发送到 Graph API 端点后,我得到了成功响应。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM