I am trying to change the password of a user in Azure AD B2C using the update endpoint provided by the Microsoft Graph API.
I followed this Microsoft documentation when I implemented this –
https://learn.microsoft.com/en-us/graph/api/user-update?view=graph-rest-1.0&tabs=http#http-request
Following is the code I used,
static async Task Main(string[] args)
{
string tenantId = "tenant-id";
string clientId = "client-id";
string clientSecret = "client-secret";
var objectId = "object-id";
var newPassword = "newPassword";
try
{
string accessToken = await GetAccessToken(tenantId, clientId, clientSecret);
await ResetPassword(accessToken, objectId, newPassword);
Console.WriteLine("Password reset successfully!");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
static async Task<string> GetAccessToken(string tenantId, string clientId, string clientSecret)
{
using (HttpClient client = new HttpClient())
{
string tokenEndpoint = $"https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/token";
var body = $"grant_type=client_credentials&client_id={clientId}&client_secret={clientSecret}&scope=https://graph.microsoft.com/.default";
var response = await client.PostAsync(tokenEndpoint, new StringContent(body, Encoding.UTF8, "application/x-www-form-urlencoded"));
var responseBody = await response.Content.ReadAsStringAsync();
var tokenJson = System.Text.Json.JsonDocument.Parse(responseBody).RootElement;
string accessToken = tokenJson.GetProperty("access_token").GetString();
return accessToken;
}
}
static async Task ResetPassword(string accessToken, string objectId, string newPassword)
{
using (HttpClient httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken}");
string graphApiEndpoint = $"https://graph.microsoft.com/v1.0/users/{objectId}";
var body = new
{
passwordProfile = new
{
forceChangePasswordNextSignIn = false,
password = newPassword
}
};
var jsonBody = System.Text.Json.JsonSerializer.Serialize(body);
var content = new StringContent(jsonBody, Encoding.UTF8, "application/json");
var response = await httpClient.PatchAsync(graphApiEndpoint, content);
var responseBody = await response.Content.ReadAsStringAsync();
response.EnsureSuccessStatusCode();
}
}
The access token was obtained for the tenant and using this access token the update endpoint in Graph API was called but a 403 error (Insufficient privileges to complete the operation) was returned.
I created an application for this console app in the Azure AD B2C and added the following permissions,
Directory.AccessAsUser.All (Delegated), Directory.ReadWrite.All (Application), User.ReadWrite.All (Application)
How can I make this work?
2
Answers
Add a User Administrator role assignment for the App registration of the application created
In application-only access, the calling app must have the
User.ReadWrite.All
application permission and must have at least theUser Administrator
Azure AD built-in role.Resources:
Azure AD built-in roles
Update user – check comment for
passwordProfile
property