简体   繁体   English

使用 Microsoft Graph + Http 客户端更改 Azure AD B2C 上的用户密码

[英]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.

相关问题 Microsoft graph 更改 azure ad b2c 用户密码不起作用 - Microsoft graph change azure ad b2c user password not working 在Azure AD B2C中使用Microsoft.Graph SDK创建本地帐户 - Create local account using Microsoft.Graph SDK in Azure AD B2C 如何在不使用Microsoft页面的情况下在Azure AD B2C中使用graph api进行社交登录? - How to use graph api for social login in Azure AD B2C without using Microsoft page? 使用Microsoft Graph更改Azure AD的密码 - Change Password for Azure AD using Microsoft Graph 如何使用 MSAL C#、.net 核心在 Azure AD B2C 用户帐户上重置密码? - How to reset password on Azure AD B2C user account using MSAL C#, .net core? 使用用户分配的托管标识访问 Azure AD B2C 和 MS Graph API - Access Azure AD B2C with MS Graph API using User-Assigned Managed Identity 带有 B2C AD 的 Azure Graph - Azure Graph with B2C AD 使用http post请求使用Graph API在Azure Active Directory(B2C)中创建新用户 - Create a new user in Azure Active Directory (B2C) with Graph API, using http post request Microsoft Graph SDK 未返回 Azure AD B2C 用户的自定义属性值 - Microsoft Graph SDK not returning Custom attributes values of Azure AD B2C Users 如何以编程方式通过 Microsoft Graph 获取 Azure AD b2c 登录日志 - how to get Azure AD b2c sign-in logs through Microsoft Graph programatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM