HTTP 401 Unauthorized - Can't load Azure Static Web Apps pages using AD B2C bearer auth access token Xamarin.Forms webview

Derek Van Tonder 41 Reputation points
2022-03-10T10:53:19.48+00:00

Hi @Anthony Chu - MSFT I am nearly finished with my Xamarin + Azure app and would greatly appreciate your insight on this troubling issue! Thank you for being such a legend so far with your great support.

I'm building a Xamarin.Forms app that loads HTML pages via HTTPS from Azure Static Web Apps using a Xamarin.Forms custom webview (derived from WkWebViewRenderer on iOS). The webview works well, except that when I enabled an "authenticated users only" role for my books directory in SWA in staticwebapp.config.json, I get a 401 Unauthorized error back from SWA when I use Active Directory B2C for authentication inside my mobile app. Here are my diagnostic steps so far:

  1. Registered Azure Static Web App and Xamarin.Forms iOS/Android app in AAD B2C (directory: 'Mental Wealth Press'). NOTE: Azure Static Web app has ADB2C app ID ending in '85f' (Azure DNS Zone: mwpress.net - DEFAULT directory)
    181795-968e4d47-2baa-4583-8b9c-a01384518403.jpeg
    181892-3fbc0463-10f6-4ace-b435-bc8af7d00009.jpeg
  2. Updated my SWA's staticwebapp.config.json to only allow authorized HTTPS requests to my 'Books' directory:
    181798-882a4fa2-4b9f-47fe-8bf2-7693d86e0add-4-5005-c.jpeg
  3. Checked that my AD B2C app registration for my Azure Static Web app had authentication turned on (I successfully retrieve an access token via MSAL later):
    181858-c49ed12d-9198-4771-af93-532c68ef0176.jpeg
    181859-2bc69d47-f873-4fa7-b479-cda1d4837540.jpeg
  4. I have also registered my Xamarin.Forms app in AD B2C, it has an app ID ending with 9fc5. This 9fc5 becomes important later because it is used as the Client ID for MSAL AcquireTokenInteractive().:
    181824-4102ca9d-15e9-4cc3-b084-cbd4f81da47f.jpeg
    181911-73f6c080-4621-493d-837a-dcf77ed5582e.jpeg
  5. Both of my AD B2C app registrations (SWA 85f and Xamarin forms 9fc5), have scopes set in AD B2C:
    181877-4f1c427b-00bd-4171-aeea-9ebb662546cc.jpeg
    181879-034bc640-bb6c-4dda-b1ad-2e1ed248f8a2.jpeg
  6. I use MSAL to grab an access token from AD B2C with the following credentials:
    https://i.imgur.com/ZJfJqqx.jpg
    https://i.imgur.com/tl6vUP4.jpg
  7. My Xamarin.Forms app successfully authenticates via Azure AD B2C, and I get back a valid access token, which looks like this. Notice that the 'audience' is for my SWA app ID ending in 85f (my app registration for Static Web App):
    https://i.imgur.com/g2j9h0r.jpg

But...when I actually try to access my pages on my SWA app using Bearer Authentication, I get a 401 Unauthorized. Here is what I get in Postman using the access token from Step 7:
https://i.imgur.com/gMqfMXn.jpg

I have also confirmed that my Xamarin app is actually sending in the right Bearer Authorization header obtained from my AD B2C access token, by loading this page ("whatismybrowser.com/detect/what-http-headers-is-my-browser-sending") with the auth header set in my Xamarin iOS custom webview:
https://i.imgur.com/raBMa4s.jpg
https://i.imgur.com/Wa6r00Q.jpg

One more test was to use the B2C App ID of my Static Web App instead of my Xamarin App to login and obtain the access token, but that throws an even stranger message, this time during B2CLOGIN. The error B2CLOGIN returns is "AADB2C90068: The provided application with ID ending in ca85f is not valid against this service. Please use application created via the B2C portal and try again" - but I did create the app registration via the B2C portal in the first place???!?!:
https://i.imgur.com/M24OEU0.jpg
https://i.imgur.com/9xd8N3w.jpg

Have absolutely no idea how to diagnose this problem, any fixes or assistance would be greatly appreciated.

Microsoft Security | Microsoft Entra | Microsoft Entra External ID
Microsoft Security | Microsoft Entra | Microsoft Entra ID
0 comments No comments
{count} votes

Accepted answer
  1. AmanpreetSingh-MSFT 56,871 Reputation points Moderator
    2022-03-15T07:11:49.823+00:00

    Hi @Derek Van Tonder • Thank you for reaching out.

    I see that you have added mwprssnet-swa-read scope to the app registration and granted admin consent. The same scope is added to the scopes array within your configuration file and is returned in the access token as well. Now, when you pass this Access Token as a bearer in the authorization header of your API call, does the API perform the authorization based on the scope present in the Access Token? I suspect you are getting the 401 error because the authorization logic is not properly implemented within your API code.

    The error AADB2C90068 error that you get when you use the App ID ending with a85f is because the application is registered as multi-tenant application which cannot be used with user flows. The application must be registered by selecting the below-highlighted option to use with B2C user flows:

    183138-image.png

    -----------------------------------------------------------------------------------------------------------

    Please "Accept the answer" if the information helped you. This will help us and others in the community as well.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.