Welcome to BigDataStacks. This blog is regarding how we can secure azure function app with azure active directory. So when we will try to access function app it will ask for login. I also elaborate on how we can access the function URL with the access token.
Let's start.
- Configure Function App
- Create an Azure Function app with anonymous access.
- Go to function app's 'Authentication / Authorization' section from 'Platform features'.
- Turn on App service Authentication/Authorization section.
- Select action 'Login with Azure AD'
- Click on Azure AD from Auth provider.
- Select 'Express' and 'create a new AD app' then click on OK.
- Click on 'Save'.
- Again open screen where we selected 'Express mode'.
- Now Select 'Advanced'. Copy 'clientId' which will be used later. NOTE: If clientId is not showing then refresh the page then it will display.
- Add one more entry in 'Allowed Token Audience'. eg. https://{your-function-app-name}.azurewebsites.net
- click on 'ok' then 'save'.
- App Registration
- Search for Azure Active Directory service.
- Go to 'App Registration'.
- Click on 'New App Registration'.
- Enter app name like 'clientApp'. Enter function app URL in redirect URIs.
- Now go to 'Overview' then copy 'Application (client) ID' & 'Directory (tenant) ID' which will be used to generate tokens.
- Click on 'Certificates & secrets' and add new client secret. It will generate a random secret. copy this 'client-secret'.
- Go to API Permissions -> Add Permission -> My APIs -> click on your function app -> give required Delegated permissions
- Test our configuration
Now our function is secured with azure directory. when you will try to access your function app url then it requires login with active directory.
We can also call our function app by fetching token and pass this token to access function app.
let's do this.
We have all the required data to fetch token eg. clientId, secret, tenantId, resourceUrl.
Make HTTP request like below. Replace all fields '{xxxxx}' details with your data. It will generate bearer token. Use this token to call your function app
POST /{tenant-id}/oauth2/token HTTP/1.1
Host: login.microsoftonline.com
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache
grant_type=client_credentials&client_id={client-id}&client_secret={client-secret}&resource=https://{your-function-app-name}.azurewebsites.net
Host: login.microsoftonline.com
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache
grant_type=client_credentials&client_id={client-id}&client_secret={client-secret}&resource=https://{your-function-app-name}.azurewebsites.net
At Superfastprocessing, we run a multi-server configuration with high-fault tolerance and load balancers. Our platform is horizontally scalable and always stays highly available for real-time data processing needs.
ReplyDelete