The goal of this repo is to provide an example of how to delegate AzureAD authentication from a frontend React SPA app to a backend Python Flask app with minimal code by leveraging Azure App Service's EasyAuth capabilities.
- Create an Azure Web App for the front end React app
- Enable authentication (EasyAuth) with AzureAD identity provider
- Select "Authentication" and then "Add Idenity Provider"
- Choose "Microsoft" as the Identity Provider
- Settings:
- App Registration Type : Create new app registration
- Supported Account Types : Current tenant - Single tenant
- Restrict Access : Require Authentication
- Unauthenticated requests : HTTP 302 Found redirect: recommended for websites
- Redirect to : Microsoft
- Token Store : Enabled
- Permissions : User.Read (default)
- Select "Add"
- Create an Azure Web App for the back end Python Flask app
- Enable authentication (EasyAuth) with AzureAD identity provider
- Select "Authentication" and then "Add Idenity Provider"
- Choose "Microsoft" as the Identity Provider
- Settings:
- App Registration Type : Create new app registration
- Supported Account Types : Current tenant - Single tenant
- Restrict Access : Require Authentication
- Unauthenticated requests : HTTP 401 Unauthorized: recommended for APIs
- Redirect to : Microsoft
- Token Store : Enabled
- Permissions : User.Read (default)
- Select "Add"
- Configure CORS for frontend app to access backend app
- Select "CORS"
- Add the FQDN of the frontend app e.g. https://frontendapp.azurewebsites.net
- Click "Save"
- Navigate to the backend app in the Azure Portal
- Select the Authentication configuration and copy the App (client) ID
- Open https://resources.azure.com/ in the browser
- Locate the frontend web app, select config > authsettingsV2
- Navigate to properties > identityProviders > azureActiveDirectory > login
- Add the below configuration and save, ensuring to replace << backend app App ID >> with the copied App ID from the backend app
"loginParameters": [
"response_type=code id_token",
"scope=openid api://<< backend app App ID >>/user_impersonation"
],
- Deploy code in "frontend" repo folder to frontend app
- Deploy code in "backend" repo folder to backend app
- Navigate to the frontend app URL in the browser
- When prompted, login to your AzureAD account
- Press F12 to open the browser developer tools and select "Console"
- You should see a console log, which is returned from the backend app, and includes:
- X-MS-CLIENT-PRINCIPAL-IDP header (Identity Provider e.g. AAD)
- X-MS-CLIENT-PRINCIPAL header (Encoded user claims)
- X-MS-CLIENT-PRINCIPAL-NAME header (authenticated user email)
If you receive 401 unauthorized errors in the console log, please hit Ctrl + F5 to fully refresh the app. This may be due to the timing of the call to the backend app and the availability of the user access token.