0
votes

Looking for an example for Azure Mobile Services developed in Xamarin Forms that is authenticated through Azure AD B2C. I have a working solution where I am able to authenticate using Azure B2C in Xamarin Forms but unable to use the resulting token in Azure Mobile Services. See code snippet below:

public static PublicClientApplication AuthenticationClient { get; private set; }
public static MobileServiceClient MobileService = new MobileServiceClient(Constants.MobileServiceClientName);                  
result = App.AuthenticationClient.AcquireTokenAsync(
                        Constants.Scopes,
                        string.Empty,
                        UIBehavior.SelectAccount,
                        string.Empty,
                        null,
                        Constants.Authority, null);
                    JObject objToken = new JObject();
objToken.Add("authenticationToken", result.IdToken);

//I am successfully able to get an Id token for Microsoft, Google and Twitter providers but when I use the token to login to my Azure Mobile Service app, I get a "Not Authorized" error

MobileServiceUser user = await MobileService.LoginAsync(MobileServiceAuthenticationProvider.MicrosoftAccount, objToken);

Any and all ideas are appreciated.

1
See code changes to the above:sidsud
See code changes to the above: objToken.Add("authenticationToken", result.IdToken); has been changed to objToken.Add("access_token", result.IdToken) and I have changed MobileServiceAuthenticationProvider.MicrosoftAccount to MobileServiceAuthenticationProvider.WindowsAzzureActiveDirectorysidsud

1 Answers

0
votes

I was able to solve the issue. The Azure AD Provider configuration on my Azure Mobile App was not configured correctly.

Here is the corrected code:

public static PublicClientApplication AuthenticationClient { get; private set; }
public static MobileServiceClient MobileService = new MobileServiceClient(Constants.MobileServiceClientName);                  
result = App.AuthenticationClient.AcquireTokenAsync(
                        Constants.Scopes,
                        string.Empty,
                        UIBehavior.SelectAccount,
                        string.Empty,
                        null,
                        Constants.Authority, null);
                    JObject objToken = new JObject();
objToken.Add("access_token", result.IdToken);

//I am successfully able to get an Id token for Microsoft, Google and Twitter providers but when I use the token to login to my Azure Mobile Service app, I get a "Not Authorized" error

MobileServiceUser user = await MobileService.LoginAsync(MobileServiceAuthenticationProvider.WindowsAzureActiveDirectory, objToken);

I can provide more details on the Azure AD configuration if anyone is interested. So now, I have a fully working Xamarin Forms Azure Mobile App that is authenticated using Azure AD B2C and so far works with Microsoft, Google and Twitter providers.