Change Password using Microsoft Graph Beta

JS Arya 26 Reputation points
2022-06-06T10:43:37.693+00:00

I am using Microsoft Graph beta and trying to reset password for the user..

It is giving me following error

"Exception of type 'Microsoft.Graph.Beta.Models.ODataErrors.ODataError' was thrown.:::: at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.ThrowFailedResponse(HttpResponseMessage response, Dictionary2 errorMapping) at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.SendAsync[ModelType](RequestInformation requestInfo, ParsableFactory1 factory, IResponseHandler responseHandler, Dictionary`2 errorMapping, CancellationToken cancellationToken)"

My Code

GraphServiceClient _graphClient = GraphHelper.GetAuthenticatedGraphClient(config, credentials);

            try
            {


                await _graphClient.Users[userId].ChangePassword.PostAsync(new Microsoft.Graph.Beta.Users.Item.ChangePassword.ChangePasswordPostRequestBody
                {
                    CurrentPassword = "Password1",
                    NewPassword = "Password2"
                });
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message + "::::" + ex.StackTrace);
            }

public static GraphServiceClient GetAuthenticatedGraphClient(IConfigurationRoot config, B2CCredentials credentials)
        {

            var scopes = new[] { "https://graph.microsoft.com/.default" };

            var options = new TokenCredentialOptions
            {
                AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
            };


            var clientSecretCredential = new ClientSecretCredential(
                credentials.B2CTenantId, credentials.ClientId, credentials.ClientSecret, options);

            var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

            return graphClient;
        }
Microsoft Security | Microsoft Entra | Microsoft Entra External ID
Microsoft Security | Microsoft Graph
0 comments No comments

3 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    10 deleted comments

    Comments have been turned off. Learn more

  2. JS Arya 26 Reputation points
    2022-06-07T07:47:41.783+00:00

    My requirement is....

    In my application there will be multiple customers, they will have their own b2c Applications defined.

    Each application will have their own users, we are creating users using Azure AD B2C and they are getting created successfully. Now the users will login to the system and from the profile page they can change the password.

    Is there any solution which can enable the b2c user to change the password?

    Was this answer helpful?


  3. Shweta Mathur 30,471 Reputation points Microsoft Employee Moderator
    2022-06-06T11:55:05.867+00:00

    Hi @JS Arya ,

    Thanks for reaching out.

    Unfortunately, B2C applications do not support graph operations. If you have registered your application in B2C tenant to authenticate with user flow, you won't be able to call Microsoft graph API endpoint.

    B2C application won't allow to add permission of your application which is required to call Graph API endpoints. It allows only "openid" and "offline_access" permissions which is not sufficient to call Graph API endpoints.

    For now, you can use the single tenant or multi-tenant option while registering your application in B2C tenant to support standard Azure AD functionality.

    To update user's password, delegated permission (Directory.AccessAsUser.All) need to add in API permissions. You can acquire the token using Authorization Code flow and use this token as bearer token to call Graph API.

    Client credential flow is not supported to update user's password as application permissions are not supported to change password which require user's interaction to provide the current password.

    Hope this will help.

    Thanks,
    Shweta

    ---------------------------------------

    Please remember to "Accept Answer" if answer helped you.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.