azureauthenticationazure-web-app-service

No sign-in screen when accessing my Azure Web App


I published an ASP.NET web app to Azure, but I'm not getting the sign-in screen.

I'm able to call the API and retrieve data when I set the Restrict access option to Allow unauthenticated access. I have followed this guide: https://learn.microsoft.com/en-us/azure/app-service/scenario-secure-app-authentication-app-service?tabs=workforce-configuration#dnl-note which states "You should be directed to a secured sign-in page, verifying that unauthenticated users aren't allowed access to the site." In my case this sign-in page is never shown, instead I just see a blank page with the text "You do not have permission to view this directory or page."

Am I missing or forgetting something?

Minimal reproducible example

Create Web app

Create Azure Web App

Publish web app

This works great, except that anyone can access this API. I would like to use Microsoft Authentication to restrict access to just the users within my organization.

Add app authentication to your web app running on Azure App Service

https://learn.microsoft.com/en-us/azure/app-service/scenario-secure-app-authentication-app-service?tabs=workforce-configuration#dnl-note

Verify access

Update

I'm able to get the sign in page by adding the following code to a desktop client application calling the API:

IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder.Create(<client-app-registration-id>)
    .WithAuthority(AzureCloudInstance.AzurePublic, <tenant-id>)
    .WithRedirectUri("http://localhost")
    .Build();

string[] scopes = [<registered-scope>];

AuthenticationResult result = await publicClientApplication.AcquireTokenInteractive(scopes).ExecuteAsync();

string accessToken = result.AccessToken;

using HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
HttpResponseMessage response = await client.GetAsync(<api-address>);

I created a new app registration for a desktop client which has a permission for a scope defined by the WebAppApi registration.

I checked the token I received and the aud, iss and appid seem correct. Still the HttpResponseMessage is StatusCode: 401, ReasonPhrase: 'Unauthorized'.


Solution

  • I got the authentication working. Thanks to Shiraz Bhaiji's answer I started looking into the token sent by the client. I started implementing my own AuthenticationHandler on the server side to see what was going on. I think one of the issues was that the authentication issuer wasn't matching: the server was expecting login.microsoftonline.com, but the client was sending sts.windows.net.

    After some googling I found a fix by changing requestedAccessTokenVersion from "null" to "2" in the App Registration manifest on Azure.