Azure Function returns 401 unauthorized after App Service Authentication is enabled

Hallman, Paul 1 Reputation point
2021-03-30T14:08:10.99+00:00

We are developing a mobile app which will call azure functions to get and post data. For our proof of concept, we have deployed a Microsoft sample java function app called HTTPExample which expects a parameter called name. The name value will appear in the response.

The function executes correctly when App Service Authentication is off. I verified using Code + Test and Postman. The authentication level is anonymous.

I'm having issues with the token after App Service Authentication is enabled. The response is 401 Unauthorized - You do not have permission to view this directory or page. This is a common issue on the message boards but haven't found a resolution that works for us.

Below are the steps I'm taking to obtain an token and execute the function app.

Using postman, my first step is to retrieve an authorization code

https://login.microsoftonline.com//oauth2/v2.0/authorize?client_id=e19cccb7-b693-486f-b4c2-286ed1f17dda&response_type=code&response_mode=query&prompt=consent&redirect_uri=https%3A%2F%2Flocalhost%3A4200%2F&scope=user.read

The authorization code is successfully returned. My second step is to get the access token using https://login.microsoftonline.com//oauth2/v2.0/token as shown in the screenshot.

82852-image.png

In the final step, I take the access_token provided in the output and pass to my function app in the header. The key is authorization and the value is Bearer . Below is a screenshot. As you can see, it failed.

I inspected the token payload. The aud and appid are the same. The value is the client id. Initially, I excluded scope so aud and appid did not match.

82807-image.png

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
Microsoft Security | Microsoft Entra | Microsoft Entra ID
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JayaC-MSFT 5,606 Reputation points
    2021-03-31T15:31:10.277+00:00

    Hello @Hallman, Paul , to isolate the issue, please navigate to https://learn.microsoft.com/en-us/rest/api/appservice/webapps/getfunction --> Click on Try it --> Provide all the mandatory inputs --> you will see an access token generated for you. Try with that token and see if that helps.

    Also, you can validate the token generate https://jwt.io/


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.