c# - 错误: Azure B2C Create user by post method from web application

标签 c# azure crud azure-ad-b2c azure-ad-graph-api

我正在尝试通过 MVC 应用程序对 Azure B2C 执行 CRUD 操作。以下应用程序用于获取用户数据,即 GET 方法。但是用于创建新用户的 POST 方法不起作用。 POST 方法给出的响应为状态代码 400,原因短语:错误请求。我在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": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="600a0f05515253030f0e13150d051220070d01090c4e030f0d" rel="noreferrer noopener nofollow">[email protected]</a>"
    }
],
"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

关于c# - 错误: Azure B2C Create user by post method from web application,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41172666/

相关文章:

python - SQLAlchemy 的快速且肮脏的 CRUD 接口(interface)?

c# - nVelocity 中 if false 的语法是什么?

c# - 我已经使用 LINQ 将 Excel 文件加载到 .NET 中,现在如何将数据批量插入到 DB (oracle) 表中?

c# - 文本属性中的全局资源

Azure 表存储查询唯一分区键

cocoa - 将核心数据模型与 Web 服务同步

c# - 在 C#.NET 中使用 USB PS2 手控器

azure - 如何在逻辑应用程序中将内容类型应用程序八位字节流转换为 csv?

Azure 网站和辅助角色

Coldfusion CRUD,必须有一个快速的方法来做到这一点