Our requirement is as follows:
- Sign-in with Microsoft account using Azure AD
- Sign-in with username and password stored in database
If I sign-in with username and password stored in database, then I have to authenticate the user and get the ID and access tokens from Azure AD.
How to achieve this?
Please help!!
We tried Azure AD and we are able to sign-in and get the tokens with all the permissions assigned to the user but we don’t know how to sign-in the username and password and get the tokens with all the permissions and role.
2
Answers
You can make use of Authorization code flow or ROPC flow to generate the access token by signing in with the username and password.
You need to register an application in Microsoft Entra and use token endpoints to generate access token if you are choosing Azure AD Authentication.
And grant permissions to the application (if you are using custom API, you can grant permission to custom API):
For sample, I granted Microsoft Graph API permission:
Generated auth code using below endpoint:
Sign in with the username and password:
Now generate the access token by using below parameters:
The above access token will contain the roles and permissions assigned to the user and you can perform actions that is access Azure resources and APIs based on your requirement.
References:
Choose a Microsoft Graph authentication provider – Microsoft Graph | Microsoft
microsoft-authentication-library-for-js/lib/msal-react at dev · AzureAD/microsoft-authentication-library-for-js · GitHub by hectormmg
Here you have a detailed tutorial written by me. Hope it helps.
Using Custom Local Auth With Azure Entra