Microsoft-Graph - As an Azure AD Admin how you can get a valid access_token for another user

nam

If I understood the response from user @MarcLaFleur here: Resetting a user's password using Microsoft Graph, if you are an Azure AD admin and want to reset a password of another user using Microsoft Graph API then you need to have a valid access_token for the user with Directory.AccessAsUser.All permission, and then you can update the user's passwordProfile.

Question: Using Microsoft Graph, as an Azure AD Admin, how can we get access_token for another user?

Authentication Page of my App Registration:

enter image description here

Joy Wang

If you are an Azure AD admin and want to reset the password of another user using Microsoft Graph API, you just need to get the token for the admin account itself, not the user you want to change.

In this case, you could use the auth code flow.

1.In your AD App, add the permissions like below -> click Grant admin consent for xxx button.

enter image description here

2.Login your admin account with the url below in the browser.

https://login.microsoftonline.com/<tenant-id>/oauth2/v2.0/authorize?
client_id=<client-id>
&response_type=code
&redirect_uri=<redirect_uri>
&response_mode=query
&scope=https://graph.microsoft.com/.default
&state=12345

enter image description here

3.Use the code to get the token.

enter image description here

4.Use the token to change the password of a normal user.

enter image description here


You could also use the Microsoft Graph SDK, use Authorization code provider.

Something like below:

IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
    .Create(clientId)
    .WithRedirectUri(redirectUri)
    .WithClientSecret(clientSecret) // or .WithCertificate(certificate)
    .Build();

AuthorizationCodeProvider authProvider = new AuthorizationCodeProvider(confidentialClientApplication, scopes);

GraphServiceClient graphClient = new GraphServiceClient( authProvider );

var user = new User
            {
                PasswordProfile = new PasswordProfile
                {
                    ForceChangePasswordNextSignIn = true,
                    Password = password,
                }
            };

await graphClient.Users[userId]
                   .Request()
                   .UpdateAsync(user);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to make Azure AD access_token compliant for its signature validation outside Microsoft Graph APIs?

How to get azure ad user properties using microsoft graph api

how to get 'code' for getting access_token when using Azure AD Graph API

How can I get a list of AD user groups along with AD user with Microsoft Graph SDK

How to Get a valid access token for my API and Microsoft Graph from Azure Active Directory?

Microsoft Azure AD graph API, How to get the user mail in response with botbuilder version 4 in node js?

How to get User Identity object from Microsoft Graph with Azure AD B2C

How to update existing user in Azure AD using Microsoft Graph API

How to use microsoft graph api for assigning role to the user in azure ad

How can I find the Admin Consent URL for an Azure AD App that requires Microsoft Graph "Read directory data" permission?

How to generate access_token for grant_type password in Azure AD for MS graph api

How to get Azure AD access token for already logged in user?

Is it possible to get all the user informations in a tenant by Azure AD Graph API or Microsoft Graph API?

Get access token from Microsoft Graph for Azure

How to request a microsoft azure resource using a access token? In short how can we use the generated access_token in azure?

Azure AD only generates invalid Access Token to use with Microsoft Graph

How to get access token without a user of microsoft azure using swift?

In the Microsoft Graph API, can an app with admin consent generate a token for a different user than the admin that approved it?

Unable to get all user list from the azure ad using Microsoft graph API

Call Microsoft Graph API to get user in Azure AD B2C

How can I access another user's resources (specifically emails) using Microsoft Graph in an MVC app?

Can you get an MSGraph access token from an Azure AD B2C sign in using Azure AD as an identity provider?

microsoft graph get access token that can be use to get user's calender

How can I access Azure Graph AND Microsoft Graph using same OAuth2 token?

How to use Microsoft Graph API to display manager of a user resource type in Azure AD

How to interact with Azure AD B2C custom User Attributes via Microsoft Graph PowerShell SDK?

Microsoft Azure AD graph API: How do I retrieve a user's email address?

Microsoft Graph SSO Azure AD not redirecting user to Login page

Forbid Azure AD User access to Microsoft Services