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

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;

}

}

34 thoughts on “Twitter, Facebook, Google and Microsoft authentication with Azure Mobile Services and Xamarin.Forms”

  1. It is actually a nice and helpful piece of info. I’m happy that you just shared this useful info with us. Please keep us up to date like this. Thank you for sharing.|

  2. Hi there, just changed into aware of your weblog via Google, and located that it is really informative. I’m gonna watch out for brussels. I’ll be grateful for those who continue this in future. Many people shall be benefited from your writing. Cheers!|

  3. Simply want to say your article is as amazing. The clarity for your publish is just spectacular and that i can suppose you’re a professional on this subject. Fine with your permission let me to seize your feed to keep updated with forthcoming post. Thanks 1,000,000 and please keep up the enjoyable work.|

  4. We’re a bunch of volunteers and starting a new scheme in our community. Your website provided us with valuable info to work on. You’ve done a formidable process and our whole neighborhood can be grateful to you.|

  5. Hi there! I could have sworn I’ve been to this blog before but after reading through some of the post I realized it’s new to me. Anyhow, I’m definitely glad I found it and I’ll be book-marking and checking back frequently!|

  6. I like the valuable info you provide in your articles. I will bookmark your weblog and check again here regularly. I am quite sure I will learn lots of new stuff right here! Good luck for the next!|

  7. When some one searches for his essential thing, thus he/she needs to be available that in detail, so that thing is maintained over here.|

  8. hello there and thank you for your information – I have certainly picked up something new from right here. I did however expertise several technical points using this website, since I experienced to reload the site a lot of times previous to I could get it to load correctly. I had been wondering if your hosting is OK? Not that I am complaining, but slow loading instances times will sometimes affect your placement in google and could damage your quality score if advertising and marketing with Adwords. Anyway I am adding this RSS to my email and can look out for much more of your respective interesting content. Ensure that you update this again soon.|

  9. I’m not that much of a online reader to be honest but your blogs really nice, keep it up! I’ll go ahead and bookmark your site to come back later on. Many thanks|

  10. Hey! I’m at work surfing around your blog from my new iphone! Just wanted to say I love reading through your blog and look forward to all your posts! Keep up the great work!|

  11. hi!,I really like your writing so a lot! percentage we be in contact more approximately your post on AOL? I require an expert in this area to unravel my problem. May be that is you! Taking a look ahead to look you. |

  12. Helpful information. Lucky me I discovered your web site by accident, and I am shocked why this accident didn’t came about in advance! I bookmarked it.|

  13. Do you mind if I quote a couple of your articles as long as I provide credit and sources back to your site? My blog is in the very same area of interest as yours and my visitors would really benefit from a lot of the information you present here. Please let me know if this okay with you. Thank you!|

  14. This is really interesting, You are a very skilled blogger. I have joined your feed and look forward to seeking more of your excellent post. Also, I’ve shared your website in my social networks!|

  15. Hi there very nice web site!! Guy .. Beautiful .. Wonderful .. I will bookmark your website and take the feeds additionally? I am happy to find a lot of useful info here in the put up, we want develop extra techniques in this regard, thanks for sharing. . . . . .|

  16. Great weblog right here! Additionally your site lots up fast! What web host are you the use of? Can I am getting your associate link for your host? I desire my web site loaded up as quickly as yours lol|

    1. Hi, i’m using Azure, I have a student subscribtion, but this is very slow, I think I will host it in a normal Azure server in the future. 😀

  17. I love your blog.. very nice colors & theme. Did you make this website yourself or did you hire someone to do it for you? Plz answer back as I’m looking to create my own blog and would like to know where u got this from. thanks

  18. I’m extremely impressed with your writing skills as well as with the layout on your weblog. Is this a paid theme or did you customize it yourself? Either way keep up the nice quality writing, it’s rare to see a great blog like this one nowadays.|

  19. I got this web site from my buddy who shared with me about this web site and now this time I am visiting this web page and reading very informative content here.|

  20. Howdy! This article could not be written any better! Looking at this post reminds me of my previous roommate! He constantly kept talking about this. I am going to send this article to him. Fairly certain he’ll have a good read. Thank you for sharing!|

  21. Hi there, simply become aware of your blog through Google, and located that it is really informative. I am going to be careful for brussels. I’ll appreciate in case you proceed this in future. Lots of other folks will probably be benefited from your writing. Cheers!|

Leave a Reply

Your email address will not be published. Required fields are marked *