我正在尝试通过 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/