Search code examples
wpfazureauthenticationazure-ad-b2cazure-app-service-envrmnt

How to Authenticate WPF Application with AAD B2C to gain access to Azure App Service


I'm quite stuck at the moment trying to implement authentication into a project I'm working on. The end goal of this project is to have two WPF apps and on web based app hosted on Azure. One WPF app is for an administrator, the other for staff, and lastly the web app for customers. Each application will be connected to one Azure App Service for a shared database and needs to have authentication so separate all the users. For authentication I am planning on using Azure Active Directory B2C.

I've been researching and trying to implement this for several days now on one of the WPF apps but as I stated before I'm quite stuck. From what I understand, the only way to do B2C authentication for WPF is through client managed authentication. Following the code shown on the Azure tutorial sites, other SO posts, and the Azure Git Repos, I have come up with the following code:

   System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
   authResult = await App.PublicClientApp.AcquireTokenAsync(App.ApiScopes,
       GetUserByPolicy(accounts, App.PolicySignUpSignIn), UIBehavior.SelectAccount, 
       string.Empty, null, App.Authority);
   Newtonsoft.Json.Linq.JObject payload = new Newtonsoft.Json.Linq.JObject();
   payload["access_token"] = authResult.AccessToken;
   MobileServiceClient msclient = new MobileServiceClient(App.AzureAppService);
   MobileServiceUser user = await msclient.LoginAsync(
        MobileServiceAuthenticationProvider.WindowsAzureActiveDirectory, payload);

Everything starts off great and I'm able to get my Sign-In policy to display. After logging in, I am given an IdToken and an AccessToken. After creating a JObject and adding the access token to it, I attempt to use it to login with my MobileServiceClient. But that's where I am having issues. No matter what I do, no matter what I try, I only get an exception with a 401 Error telling me I'm unauthorized. And this is the point I've been stuck at for the past few days.

Obviously I'm not doing anything special here and I imagine many people have done this before me but I just can't seem to get past this point and was hoping someone may be able to offer me some guidance. Am I way off track? Is there a better way that I could be doing this? Any suggestions or advice would be greatly appreciated as I am very new to Azure.

Thanks all!

Update:

Here's how I have my Azure Settings:

On the app service side

Client Id: "{Client Id of the AAD B2C App}"
Issuer URL: "login.microsoft.com{TennatName}.onmicrosoft.com/v2.0/.well-known/openid-configuration"
Allowed Token Audiences: "https://{App Service Name}.azurewebsites.net" (App Service URL)

On B2C side:

Web and native client enabled
Web Reply URL: "https://{AppServiceName}.azurewebsites.net/.auth/login/add/callback"
Native App: I did not know what custom redirect URL to have so I have both
"{TennatName}.onmicrosoft.com://auth/" and 
"{AppServiceName}.azurewebsites.net/.auth/login/add/callback"

Update 2:

My authority is login.microsoftonline.com/tfp{tenant}/{policy}/oauth2/v2.0/authorize
And my ApiScopes = { "https://{Tenant}/thisisatest/user_impersonation" };

Solution

  • If the authority for the client is set to https://{your-tenant-name}.b2clogin.com/tfp/{your-tenant-name}.onmicrosoft.com/{your-policy-name}/, then the issuer URL in the app service must refer to the metadata for this authority; i.e. https://{your-tenant-name}.b2clogin.com/tfp/{your-tenant-name}.onmicrosoft.com/{your-policy-name}/v2.0/.well-known/openid-configuration.