ERROR: Azure B2C Create user by post method from web application

ERROR: Azure B2C Create user by post method from web application

我正在尝试通过 MVC 应用程序对 Azure B2C 执行 CRUD 操作。以下应用程序用于获取用户数据,即 GET 方法。但是创建新用户的 POST 方法不起作用。 POST 方法给出响应 Status Code 400,原因 Phrase:Bad Request。我在GitHub的项目中添加了这两个方法来实现CRUD功能:Microsoft Graph SDK ASPNET Connect

public async Task<string> UpdateUserDetails(string accessToken)
    {
        string jsondata = File.ReadAllText("C:\usertemplate-email.json");

        string endpoint = "https://graph.microsoft.com/v1.0/users";
        using (var client = new HttpClient())
        {
            using (var request = new HttpRequestMessage(HttpMethod.Post, endpoint))
            {
                request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                request.Content = new StringContent(JsonConvert.SerializeObject(jsondata), Encoding.UTF8, "application/json");
                //didn't received successStatusCode with or without searilizing jasondata
                using (HttpResponseMessage response = await client.SendAsync(request))
                {
                    if (response.IsSuccessStatusCode)
                    {
                        //Status Code: 400, Reason Phrase:Bad Request
                        var json = JObject.Parse(await response.Content.ReadAsStringAsync());

                    }
                    return response.ReasonPhrase;
                }
            }
        }

    }

    public async Task<string> GetUserDetails(string accessToken)
    {

        // Get the current user. 
        // The app only needs the user's email address, so select the mail and userPrincipalName properties.
        // If the mail property isn't defined, userPrincipalName should map to the email for all account types. 
        string endpoint = "https://graph.microsoft.com/v1.0/users/4b4442a8-bd02-4992-9c62-b1b31807428d";


        using (var client = new HttpClient())
        {
            //if no queryParameter parameter is passed, all parameters will be selected
            using (var request = new HttpRequestMessage(HttpMethod.Get, endpoint))
            {
                request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                using (HttpResponseMessage response = await client.SendAsync(request))
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        var json = JObject.Parse(await response.Content.ReadAsStringAsync());
                      //got the expected result at this point
                    }
                    return response.ReasonPhrase;
                }
            }
        }
    }

包含的 json 文件:

{
"accountEnabled": true,
"signInNames": [
    {
        "type": "emailAddress",
        "value": "joe123consumer@gmail.com"
    }
],
"creationType": "LocalAccount",
"displayName": "Joe Consumer",
"mailNickname": "joec",
"passwordProfile": {
    "password": "P@ssword!",
    "forceChangePasswordNextLogin": false
},
"passwordPolicies": "DisablePasswordExpiration",


"city": "San Diego",
"country": null,
"facsimileTelephoneNumber": null,
"givenName": "Joe",
"mail": null,
"mobile": null,
"otherMails": [],
"postalCode": "92130",
"preferredLanguage": null,
"state": "California",
"streetAddress": null,
"surname": "Consumer",
"telephoneNumber": null
 }

可能是因为您使用的是 Microsoft Graph API,而不是 Azure AD Graph API。

据我所知,Microsoft Graph API 不适用于 B2C。

您想使用这个:https://msdn.microsoft.com/en-us/library/azure/ad/graph/api/users-operations

请确保您要添加的账号之前没有在租户上退出。

当我第一次添加帐户时,JSON 对我来说效果很好。当我尝试再次添加时,我可以重现此问题并出现 400 错误。这是详细的错误消息:

我们可以使用Fiddler抓取请求,查看详细错误信息。

最后,我能够按照 microsoft graph api documentation 中建议的参数完成它。 我使用的json数据是: {"accountEnabled":真,<br> "displayName": "my, name",<br> "mailNickname": "emailname",<br> "userPrincipalName": "tokigston@yourdomain.onmicrosoft.com",<br> "passwordProfile" : {<br> "forceChangePasswordNextSignIn":正确,<br> "password": "P@ssword!"<br> } }

mailNickname 属性值中不应有 space。 CreateuserFunction 将是:

public async Task<string> CreateUser(string accessToken)
    {
        string jsondata = File.ReadAllText(@"C:\usertemplate-email.json");

        string endpoint = "https://graph.microsoft.com/v1.0/users";
        using (var client = new HttpClient())
        {
            var method = new HttpMethod("POST");

            using (var request = new HttpRequestMessage(method, endpoint))
            {
                request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                request.Content = new StringContent(jsondata.ToString(), Encoding.UTF8, "application/json");
                using (HttpResponseMessage response = await client.SendAsync(request))
                {
                    if (response.IsSuccessStatusCode)
                    {
                        //Status Code: 400, Reason Phrase:Bad Request
                        //On success, it gives Created
                        var json = JObject.Parse(await response.Content.ReadAsStringAsync());

                    }
                    return response.ReasonPhrase;
                }
            }
        }
    }