azureazure-active-directoryazure-functionsazure-ad-msalimplicit-flow

Easy Auth Implicit Flow with AAD and Azure Function returns Unauthorized


API repro

client repro

Problem

My API request is unauthorized. I got status 401:

POST https://{azure func name}.azurewebsites.net/api/HttpTrigger1?code={code}== 401 (Unauthorized)

Http address was copied from azure portal.

When I click link, I am redirected to log in and next redirected to to azure function with my "Hello from secured API" text in brwoser tab.

I seems that Easy auth treat my azure function as Client App, whether I want it to be middleware APi for my vue.js app. My Azure function API should only recieve and validate access tokens.

Tried

I also tried to sent request from postman with the token, but also received unauthorized.

I explored the token and it seems to be correct:

"aud": "api://{Azure function App Id (id in AAD)}",
"iss": "https://sts.windows.net/{tenant id}/",
"appid": "{registered vue js client app id (id in AAD)}",

Solution

  • To resolve the problem I had to go to Authentication/Authorization section in my Azure Function App platform features. Under Authentication provider select AAD -> advanced and add allowed token audience: api://{Azure Function AD App Id }.

    The problem was that my token was intended for other audience: https://{func name}.azurewebsites.net. It was due to changing my AF AD App Id:

    "In Expose Api section I changed App Id Uri from https://{name}.azurewebsites.net to api://{app id} in order to choose multitenant supported client types. There already has been user_impersonation scope defined."

    This operation also has changed scope names.