问题
I am doing azure active directory authentication for function app by following below URL https://www.c-sharpcorner.com/article/secure-azure-function-with-azure-ad/
But when i hitting my url through function app It is giving unauthorized do i need to do anything else
please find the below screen shots.
any help is appreciated
回答1:
We could use the access token to access your Azure function API directly if your azure function authentication level is anonymous or function key is also required. The detailed steps are as below.
- Configure Azure AD for Azure Function. Please refer to https://docs.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings#auth.
- Get Access token
I get an access token from AAD using clientID and clientSecret using the following code:
METHOD: POST
Url : https://login.microsoftonline.com/your directory ID/oauth2/token
HEADERS: Content-Type : application/x-www-form-urlencoded
BODY: grant_type+=client_credentials&resource+=”your Function url”&client_id+++++=”the application that your register id”&client_secret+=”the key you create”
Please note that according to the Azure Resources portal (https://resources.azure.com/), the default allowedAudiences is
https://{functionAppName}.azurewebsites.net/.auth/login/aad/callback
So I added the https://{functionAppName}.azurewebsites.net/ as allowed audiences.
Then I can use the access token directly. I tested it with Postman:
Please note that if your azure function authentication level is not anonymous, you need to add the function key
来源:https://stackoverflow.com/questions/55022474/azure-active-directory-authentication-function-app