Hi @Sachin
Your code is trying to update another user's password and not the user's own password, you should call the /me endpoint to reset the self password for the user. Please refer to my complete code:
using Azure.Identity;
using Microsoft.Graph;
var scopes = new[] { "Directory.AccessAsUser.All" };
var tenantId = "{tenant id}";
var clientId = "{client id}";
var clientSecret = "{client secret}";
// For authorization code flow, the user signs into the Microsoft
// identity platform, and the browser is redirected back to your app
// with an authorization code in the query parameters
var authorizationCode = "authorization code";
// using Azure.Identity;
var options = new TokenCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};
// https://learn.microsoft.com/dotnet/api/azure.identity.authorizationcodecredential
var authCodeCredential = new AuthorizationCodeCredential(
tenantId, clientId, clientSecret, authorizationCode, options);
var graphClient = new GraphServiceClient(authCodeCredential, scopes);
var currentPassword = "OldPassword";
var newPassword = "NewPassword";
await graphClient.Me
.ChangePassword(currentPassword, newPassword)
.Request()
.PostAsync();
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.