[英]Change user password on Azure AD B2C using Microsoft Graph + Http Client
I've this code...我有这个代码...
public class PasswordProfile {
public bool forceChangePasswordNextSignIn { get { return false; } }
public string password { get; set; }
}
protected async Task setPasswords(string newP)
{
PasswordProfile newPassword = new PasswordProfile()
{
password = newP
};
var content = new StringContent(JsonSerializer.Serialize(newPassword), Encoding.UTF8, "application/json");
Http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", appToken);
var update = await Http.PatchAsync($"https://graph.microsoft.com/v1.0/users/{userUUID}", content);
}
I can do this scenario using Postman.我可以使用 Postman 来完成这个场景。 I just set the method to patch, put the json as body and it works good by returning a 204 and the password realy changed.
我只是将方法设置为补丁,将 json 作为主体,返回 204 并且密码确实更改了,效果很好。
However my code return a 404. Any clue?但是我的代码返回 404。有什么线索吗?
When you call JsonSerializer.Serialize(newPassword)
it returns the following json当您调用
JsonSerializer.Serialize(newPassword)
它返回以下 json
{
"forceChangePasswordNextSignIn": false,
"password": "xWwvJ]6NMw+bWH-d"
}
but Graph API endpoint expects json like this但是图形 API 端点期望像这样的 json
{
"passwordProfile": {
"forceChangePasswordNextSignIn": false,
"password": "xWwvJ]6NMw+bWH-d"
}
}
You can create a new class User
with PasswordProfile
property and serialize an instance of User
class.您可以使用
PasswordProfile
属性创建新的 class User
并序列化User
class 的实例。
public class User
{
public PasswordProfile passwordProfile { get; set; }
}
protected async Task setPasswords(string newP)
{
var user = new User
{
passwordProfile = new PasswordProfile
{
password = newP
}
}
var content = new StringContent(JsonSerializer.Serialize(user), Encoding.UTF8, "application/json");
Http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", appToken);
var update = await Http.PatchAsync($"https://graph.microsoft.com/v1.0/users/{userUUID}", content);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.