My goal is to have Angular 8 SPA with a serverless backend represented by multiple azure function apps using OAuth from Facebook, Google...
I have a problem with calling azure functions on behalf of authorized users. Function sees these calls as anonymous users.
From the browser, function returns authorized user name, from browser app call it returns 'no name' that mean user is not authorized.
My guess is that the session from myapp.azurewebsites.net is not visible in my app that is in the localhost (it can be any other domain). Also, I can't provide session in the request to function endpoint.
So, what is the way to authorize users and call azure functions from the client app in other domain? Or it is possible only with the manual implementation of JWT tokens with spreaded logic across all functions? Also, I do not want to pay to third-party services like Auth0 or even AAD.
Jim Xu, suggested a way that works, but not for my case. Disadvantages I see:
- With this approach, I can't see the user name and email in the claims principal. Also not sure what can I use as user Id.
- Login logic will be spread across all apps that are using the API.
- Will need to store FB token to authenticate all function apps
I am looking for answers to such questions:
- Is there backend driven authentication for my case?
- Is there an ability to setup post_login_redirect_url to receive a token for service also?
- Maybe it is possible to set up via https://resources.azure.com/ ?
- Is it possible to share access token for multiple function apps? (In this way, UI logic will be simplified to make get to app/.auth/login/provider and then save a service token.)
My code sample and configurations:
Here is a client app main parts I am using to call:
<button (click)="call('https://myapp.azurewebsites.net/Function1')">CallWithoutAuth</button>
<button (click)="call('https://myapp.azurewebsites.net/Function2')">CallWithAuth</button>
<a href="https://myapp.azurewebsites.net/.auth/login/facebook?post_login_redirect_url=http%3A%2F%2Flocalhost%3A5000" target="_blank">Log in with Facebook</a>
Body of call function:
var url = 'my azure func url with auth via facebook';
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText);
}
};
xhttp.onerror = function(e){console.log(e, this)};
xhttp.open("GET", url, true);
xhttp.send();
Function code:
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "Func2")] HttpRequest req,
ClaimsPrincipal claimsPrincipal)
{
var name = claimsPrincipal?.Identity?.Name;
return (ActionResult)new OkObjectResult($"Hello, {name ?? "no name"}");
}
Here are functions app configs:
CORS config:
Fasebook config:




