1
votes

Is it possible to call an AAD authenticated Azure function from javascript without an auth library like ADAL and also without registering the client application with Microsoft?

Getting this error: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '

Both the simple web client app and azure function are registered under the same AAD. Both have the azurewebsites.net domain.

What's the lightest web client we can have?

2
You can call without ADAL, but it's not possible without registering application on Azure AD - cuongle
without any auth library? We've been trying with no luck, any samples you know of? - Hell.Bent
Not sure is there any sample on github, but it's just OpenID Connect, how have you tried? - cuongle
We have both the client web app and functions app registered under the same AAD but get No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' . - Hell.Bent
Please try to use the implicit flow with response_type=token or response_type=id_token to get tokens directly from the authorize endpoint . Azure ad endpoints don't support CORS . - Nan Yu

2 Answers

0
votes

The error you are getting is coming from a cross-origin resource sharing (CORS) check. I suspect this occurs when calling the function from the web app. The idea is that the browser is making an OPTIONS request first to see if the caller (the web app) is allowed to make a call a resource on a different domain (the function app). If that's approved, then it will make the actual call to the function.

So, we just have to make it so that the function app responds letting the browser know the call is allowed. Fortunately, Functions has a built-in CORS feature. In the portal, select Platform features for your function app. Under the API section, you'll see a CORS option. Add the domain for your function app and click Save. You should see the Access-Control-Allow-Origin error go away.

As for AAD, any OpenID Connect client library would work - ADAL is a fine choice for this, though. You may still need to create a client registration, though.

0
votes

In Azure AD ,with the normal OpenID Connect/OAuth flow, you would acquire token by making a request to the /token endpoint. However, the azure ad endpoint does not support CORS requests, so making AJAX calls to get access tokens is out of the question. Instead, you can use the implicit flow in a hidden iframe to get new tokens for web APIs . See document here and here for more details .

And yes,i would suggest you use ADAL.JS which helps you to use Azure AD for handling authentication easier .