Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Function with AD auth results in 401 Unauthorized when using Bearer tokens

Tags:

I have a very simple Azure function in C# for which I've setup Azure AD Auth. I've just used the Express settings to create an App registration in the Function configuration.

public static class IsAuthenticated {     [FunctionName("IsAuthenticated")]     public static async Task<IActionResult> Run(         [HttpTrigger(AuthorizationLevel.Anonymous, "get", "options", Route = null)]         HttpRequest req,         ILogger log)     {         return new OkObjectResult("You are " + req.HttpContext.User.Identity.Name);     } } 

When I access the function in my browser everything works as expected (if not logged in I have to login and get redirected to my API). But if I try to access my function anywhere a Bearer token is needed I get an 401 Unauthorized error. Even weirder I also can't execute the function in the Azure Portal.

But the token was aquired without a problem and added to the request:

enter image description here

I've tried a few different things to solve this problem. First I thought maybe it's a CORS problem (since I've also had a few of those) and just set CORS to accept *, but nothing changed.

Then I've added my API login endpoints to the redirect and tried setting the implicit grant to also accept Access tokens, it's still not working.

enter image description here

Is there anything I've overlooked? Shouldn't the App registration express config just work with azure functions?

EDIT:

Putting the URL to my function app in the redirects as suggested by @thomas-schreiter didn't change anything (I've tried the config in the screenshot and also just putting each of those values on it's own).

enter image description here

EDIT 2:

I've now also tried to aquire an Bearer token the manual way with Postman, but I still run into a 401 when calling my API.

like image 585
Staeff Avatar asked Mar 18 '19 16:03

Staeff


People also ask

What is authorization level in Azure functions?

Azure HTTP authorization level, Determines what keys, if any, need to be present on the request in order to invoke the function.

What is OAuth token in Azure?

The OAuth 2.0 authorization code grant type, or auth code flow, enables a client application to obtain authorized access to protected resources like web APIs. The auth code flow requires a user-agent that supports redirection from the authorization server (the Microsoft identity platform) back to your application.


2 Answers

UPDATE 2020-05-12: According to ambrose-leung's answer further below you can now add a custom issuer URL which should potentially enable you to use v2 tokens. I haven't tried this myself, but maybe this will provide useful for someone in the future. (If his answer helped you please give him an upvote and maybe leave a comment 😉)


This took forever to figure out, and there is very little information about this in the offical documentations.

But it turns out the problem was/is that Azure Functions don't support Bearer tokens generated by the oauth2/v2.0/ Azure API. Since the portal uses those (if your AD supports them) you are out of luck to be able to run the function in there.

This also explains why my postman requests didn't work, because I was also using the v2 api. After switching to v1 I could access my API (Postman doesn't allow you to add a resource_id when you use the integrated auth feature, so I had to switch to handling everything manually).

After that came the realisation that you can't use MSAL either if you are writing a JS client (Angular in my case). So one alternative is ADAL, where the Angular implementation looks kind of awkward. So I decided to use angular-oauth2-oidc which took another hour of tinkering to get it to play nicely with Azure AD.

But after all that I can finally access my API.

I really don't understand why you wouldn't allow users to access Azure Function Apps with Azure AD v2 tokens, but at least this should be so much better documented. But whatever, I can finally go to sleep.

EDIT: After I opend an issue for this, they added a note that v2 isn't supported by Azure Functions, hopefully making life easier for other people.

https://docs.microsoft.com/en-us/azure/app-service/configure-authentication-provider-aad

like image 100
Staeff Avatar answered Sep 18 '22 11:09

Staeff


I managed to get it working through postman using following configuration. Important lesson was setting in "Allowed token audiences" and "resource" name used in postman to acquire token should be same in this case. I used the same code provided here in question. in this case app registered in Azure AD is a client and resource as well. configuration and testing through postman as follows

enter image description here

Acquire token in postman

enter image description here

Calling azure function using Postman .. Authorization header with bearer token

enter image description here

like image 32
Rahul Ruikar Avatar answered Sep 20 '22 11:09

Rahul Ruikar