2020.09.01.

Twitter, Facebook, Google and Microsoft authentication with Azure Mobile Services and Xamarin.Forms

By bence960206

Install the whole Solution from extending the Microsoft.Azure.Mobile.Client NuGet packaget.

Go to the Azure portal and create a Mobile App Service. Go to the Authentication tab.

Note that: We have to register our app on theese websites and after that we have to fill on the portal the AppIDs and AppSecrets.

Microsoft: https://apps.dev.microsoft.com/

Facebook: https://developers.facebook.com/apps/

Google: https://console.developers.google.com/apis/dashboard/

Twitter: https://apps.twitter.com/

Note: the redirect URIs should look like:

<azure mobile service URL>/.auth/login/<facebook,google,twitter,microsoftaccount>/callback

Example: https://httpbasicauth.azurewebsites.net/.auth/login/facebook/callback

1. Add this Interface to our Portable project.

public interface IAuthenticate {     Task<bool> Authenticate(); }

2. Add this event to our portable, create a button and subscribe to event (For example add this to our App.cs).

public static IAuthenticate Authenticator { get; private set; }

public static void Init(IAuthenticate authenticator)   {

Authenticator = authenticator;

}

async void loginButton_Clicked(object sender, EventArgs e) {

bool success await App.Authenticator.Authenticate();

}

Finally we have to add to our platforms the platform specific functions(implements).

UWP: MainPage.cs

using Microsoft.WindowsAzure.MobileServices;

using System.Threading.Tasks;

using Windows.UI.Popups;

using static <your app name>.App;

using System.Diagnostics;

public sealed partial class MainPage : IAuthenticate

{

private MobileServiceUser user;

private MobileServiceClient client;

public async Task<bool> Authenticate()

{

string message = string.Empty;

var success = false;

try

{

// Sign in with Facebook login using a server-managed flow.

if (user == null)

{

client = new MobileServiceClient(“your azure mobile service url”);

user = await client

.LoginAsync(MobileServiceAuthenticationProvider.Twitter);

if (user != null)

{

Debug.WriteLine(user.UserId);

Debug.WriteLine(user.MobileServiceAuthenticationToken);

success = true;

message = string.Format(“You are now signed-in as {0}.”, user.UserId);

}

}

}

catch (Exception ex)

{

message = string.Format(“Authentication Failed: {0}”, ex.Message);

}

// Display the success or failure message.

await new MessageDialog(message, “Sign-in result”).ShowAsync();

return success;

}

public MainPage()

{

this.InitializeComponent();

ttwitter.App.Init(this);

LoadApplication(new ttwitter.App());

}

}

Android: AppDelegate.cs

using Microsoft.WindowsAzure.MobileServices;

using System.Threading.Tasks;

using static <your app name>.App;

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity, IAuthenticate

{

private MobileServiceUser user;

private MobileServiceClient client;

public async Task<bool> Authenticate()

{

var success = false;

var message = string.Empty;

try

{

// Sign in with Facebook login using a server-managed flow.

client = new MobileServiceClient(“your azure mobile service url”);

user = await client

.LoginAsync(this,MobileServiceAuthenticationProvider.Twitter);

if (user != null)

{

message = string.Format(“you are now signed-in as {0}.”,

user.UserId);

success = true;

}

}

catch (Exception ex)

{

message = ex.Message;

}

// Display the success or failure message.

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.SetMessage(message);

builder.SetTitle(“Sign-in result”);

builder.Create().Show();

return success;

}

protected override void OnCreate(Bundle bundle)

{

TabLayoutResource = Resource.Layout.Tabbar;

ToolbarResource = Resource.Layout.Toolbar;

base.OnCreate(bundle);

App.Init((IAuthenticate)this);

global::Xamarin.Forms.Forms.Init(this, bundle);

LoadApplication(new App());

}

}

iOS: AppDelegate.cs

using Microsoft.WindowsAzure.MobileServices;

using System.Threading.Tasks;

using static <your app name>.App;

public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate, IAuthenticate

{

public override bool FinishedLaunching(UIApplication app, NSDictionary options)

{

App.Init(this);

global::Xamarin.Forms.Forms.Init();

LoadApplication(new App());

return base.FinishedLaunching(app, options);

}

// Define a authenticated user.

private MobileServiceUser user;

private MobileServiceClient client;

public async Task<bool> Authenticate()

{

var success = false;

var message = string.Empty;

try

{

// Sign in with Facebook login using a server-managed flow.

if (user == null)

{

client = new MobileServiceClient(“your azure mobile service url”);

user = await client

.LoginAsync(UIApplication.SharedApplication.KeyWindow.RootViewController,

MobileServiceAuthenticationProvider.Twitter);

if (user != null)

{

message = string.Format(“You are now signed-in as {0}.”, user.UserId);

success = true;

}

}

}

catch (Exception ex)

{

message = ex.Message;

}

// Display the success or failure message.

UIAlertView avAlert = new UIAlertView(“Sign-in result”, message, null, “OK”, null);

avAlert.Show();

return success;

}

}