Forum Discussion

BrynnerFlores's avatar
BrynnerFlores
Copper Contributor
Feb 21, 2024

How to reset a user's password using the Microsoft Graph API in C#?

I am trying to reset a user's Office 365 account password using the Microsoft Graph API with C#, the permissions I have assigned are as follows:

 

This is an attempt I made in PowerShell:

 

In the $params variable I pass the new password and then call the Update-MgUser cmdlet, this runs without a problem, but when I try to do this using the API in C# I get the following error:

 

It tells me that I have insufficient privileges to complete the operation, but in PowerShell I can perform the operation normally, the code is as follows:

 

3 Replies

  • How are you authenticating? PowerShell uses its own application registration for authentication, and therein you have likely already consented to the required permissions. You need to do the same for the application you are using in your custom code.
      • VasilMichev's avatar
        VasilMichev
        MVP
        So make sure the application you're leveraging for the client credentials flow has the necessary permissions. You can decode the token via jwt.ms and similar tools and confirm the presence of User.ReadWrite.All. Also, if the target user holds any admin roles, the service principal will need to have the Privileged auth admin role assigned.

Resources