0

I'm trying to get a refresh token set up in my Xamarin.Forms app using AAD B2C. I've got everything set up but run into issues when calling LoginAsync on my MobileServiceClient. All of the docs and examples I can find show to update my LoginAsync method to this:

var user = await App.MobileServiceClient.LoginAsync(MobileServiceAuthenticationProvider.WindowsAzureActiveDirectory,
    new Dictionary<string, string>() { { "response_type", "code id_token" } });

Except that the MobileServiceClient does not take a Dictionary<string, string> for the second parameter. It takes a JObject. Here's what my current code looks like:

var authResult = await App.AuthenticationClient.AcquireTokenAsync(Constants.Scopes, "", UiOptions.SelectAccount, string.Empty, null, Constants.Authority, Constants.Policy);

var payload = new JObject();
payload["access_token"] = authResult.Token;

var user = await App.MobileServiceClient.LoginAsync(MobileServiceAuthenticationProvider.WindowsAzureActiveDirectory, payload);

I can not find an example use the JObject anywhere.
It is as simple as adding payload["response_type"] = "code id_token"; to my payload?

Bruce Chen
  • 18,207
  • 2
  • 21
  • 35
Marcus
  • 5,407
  • 3
  • 31
  • 54

1 Answers1

0

AFAIK, Mobile Apps support two authentication flows (client-managed flow and server-managed flow).

Client-managed authentication

Your app can independently contact the identity provider and then provide the returned token during login with your backend. This client flow enables you to provide a single sign-on experience for users or to retrieve additional user data from the identity provider.

After you retrieved the token, then you would login with your azure mobile backend by passing the token into a JObject instance as follows:

JObject payload = new JObject();
payload["access_token"] = ar.AccessToken;
var user = await client.LoginAsync(MobileServiceAuthenticationProvider.WindowsAzureActiveDirectory, payload);

For more details about other identity providers via client-flow authentication, you could refer to Client-managed authentication.

Server-managed authentication

Your app directly contacts your mobile backend, then your azure mobile backend contacts the identity provider and provide you with the logged user.

  • For Xamarin.Forms UWP app, you could login as follows:

enter image description here

  • For Xamarin.Forms IOS app, you could login as follows:enter image description here

For more details about server-managed authentication in Xamarin.Forms, you could refer to Add authentication to your Xamarin Forms app.

UPDATE:

I have checked that if you call MobileServiceClient.LoginAsync in PCL, you could not see any extensions for LoginAsync. As you could see, there are many extension LoginAsync methods in the Microsoft.WindowsAzure.Mobile.Ext.dll for each platform. You need to define the IAuthenticate interface and implement it in each of your app (uwp, android, ios, etc.), for more details you could refer to here.

Bruce Chen
  • 18,207
  • 2
  • 21
  • 35
  • Are those screenshots from a live project of yours? Because my version of Microsoft.Azure.Mobile.Client is `portable-win+net45+wp8+wpa81+monotouch+monoandroid\Microsoft.WindowsAzure.Mobile.dll`, and that's the latest full release from NuGet. And it does not have the LoginAsync() with the Dictionary parameter. – Marcus Jun 07 '17 at 18:49
  • 1
    And it looks like the only difference between server and client flow is that the client flow uses the token from the `AcquireTokenAsync()` method and passes it in the `payload` in the `LoginAsync()` method. – Marcus Jun 07 '17 at 18:53
  • According to your comment, I assumed that you call the `MobileServiceClient.LoginAsync` in your PCL. As you could see, there are many extension `LoginAsync` methods in the `Microsoft.WindowsAzure.Mobile.Ext.dll` for each platform. You need to define the `IAuthenticate` interface and implement it in each of your app (uwp, android, ios, etc.), for more details you could refer to [here](https://learn.microsoft.com/en-us/azure/app-service-mobile/app-service-mobile-xamarin-forms-get-started-users#add-authentication-to-the-portable-class-library). – Bruce Chen Jun 08 '17 at 01:23