I registered one Azure AD B2C application and added API permissions as below:
Now, I assigned User Administrator role to that application like this:
When I ran below c# code to reset B2C user’s password using client credentials flow, I got same error even after assigning User Administrator role:
using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Graph.Models;
using Microsoft.Kiota.Abstractions;
using Microsoft.Graph.Models.ODataErrors;
using System;
var scopes = new[] { "https://graph.microsoft.com/.default" };
var clientId = "appId";
var tenantId = "tenantId";
var clientSecret = "secret";
var options = new ClientSecretCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
};
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret, options);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
try
{
var requestBody = new Microsoft.Graph.Users.Item.Authentication.Methods.Item.ResetPassword.ResetPasswordPostRequestBody
{
NewPassword = "xxxxxx",
};
var result = await graphClient.Users["userId"].Authentication.Methods["28c10230-6103-485e-b985-444c60001490"].ResetPassword.PostAsync(requestBody);
Console.WriteLine("Password changed successfully!");
}
catch (ODataError odataError)
{
Console.WriteLine(odataError.Error.Code);
Console.WriteLine(odataError.Error.Message);
}
Response:
Note that, client credentials flow works with permissions of Application type only. Resetting password operation does not
support Application permissions as mentioned in this MS Doc.
Alternatively, you can run Update user query to reset B2C user’s
password by updating passwordProfile property.
When I ran below c# code by running PATCH query, user’s password changed successfully with below response:
using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Graph.Models;
using Microsoft.Kiota.Abstractions;
using Microsoft.Graph.Models.ODataErrors;
using System;
var scopes = new[] { "https://graph.microsoft.com/.default" };
var clientId = "appId";
var tenantId = "tenantId";
var clientSecret = "secret";
var options = new ClientSecretCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
};
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret, options);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
try
{
var requestBody = new User
{
PasswordProfile = new PasswordProfile
{
ForceChangePasswordNextSignIn = false,
Password = "xxxxxxxxx",
},
};
var result = await graphClient.Users["userId"].PatchAsync(requestBody);
Console.WriteLine("Password changed successfully!");
}
catch (ODataError odataError)
{
Console.WriteLine(odataError.Error.Code);
Console.WriteLine(odataError.Error.Message);
}
Response:
In your case, change your code to run PATCH query by updating passwordProfile property along with assigning User administrator role to the application.
2
Answers
As per this:
Note
For the application to update user account passwords, you’ll need to grant the user administrator role to the application.
I registered one Azure AD B2C application and added API permissions as below:
Now, I assigned User Administrator role to that application like this:
When I ran below c# code to reset B2C user’s password using client credentials flow, I got same error even after assigning User Administrator role:
Response:
When I ran below c# code by running
PATCH
query, user’s password changed successfully with below response:Response:
In your case, change your code to run
PATCH
query by updatingpasswordProfile
property along with assigning User administrator role to the application.Reference:
Update user – Microsoft Graph