Click here to Skip to main content
Click here to Skip to main content

Tagged as

Using Xamarin.Auth.OAuth2Authenticator with async/await

, 30 Jun 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Using Xamarin.Auth.OAuth2Authenticator with async/await

Since Xamarin published the Xamarin 3 I have been playing with this.

Normally I develop apps for Windows Phone and Windows Store, and when i started to use some libraries related with Xamarin, I found some that libraries don´t use the async/awaitconcept, and for me is a fundamental.

One library that I think should use async/await but that I saw don´t use is the Xamarin.Auth!

For how that uses Xamarin.Auth.OAuth2Authenticator for authentication I recommend this solution:

For Xamarin.Android

public async Task<AuthenticatorCompletedEventArgs> LoginAsync(Activity activity, bool allowCancel)
        {
             var auth = new OAuth2Authenticator(
               clientId: "<scopes here>",
               scope: "<scopes here>",
               authorizeUrl: new Uri("<url here>"),
               redirectUrl: new Uri("<url here>"))
            {
                AllowCancel = allowCancel
            };

            // If authorization succeeds or is canceled, .Completed will be fired.
             var tcs1 = new TaskCompletionSource<AuthenticatorCompletedEventArgs>();
            EventHandler<AuthenticatorCompletedEventArgs> d1 =
                (o, e) =>
                {
                    try
                    {
                        tcs1.TrySetResult(e);
                    }
                    catch (Exception ex)
                    {
                        tcs1.TrySetResult(new AuthenticatorCompletedEventArgs(null));
                    }
                };
            auth.Completed += d1;
            var intent = auth.GetUI(activity);
            activity.StartActivity(intent);
            var result = await tcs1.Task;
            auth.Completed -= d1;
            return result;
        }

And then we need to call it this way:

var authService = new AuthService();
var result = await authService.LoginAsync(this, allowCancel);

For Xamarin.IOS

public async Task<Account> LoginAsync(DialogViewController dialog, bool allowCancel)
        {

            var auth = new OAuth2Authenticator(
               clientId: "<scopes here>",
               scope: "<scopes here>",
               authorizeUrl: new Uri("<url here>"),
               redirectUrl: new Uri("<url here>"))
            {
                AllowCancel = allowCancel
            };

            // If authorization succeeds or is canceled, .Completed will be fired.
            var tcs1 = new TaskCompletionSource<AuthenticatorCompletedEventArgs>();
            EventHandler<AuthenticatorCompletedEventArgs> d1 =
                (o, e) =>
                {
                    try
                    {
                        tcs1.TrySetResult(e);
                    }
                    catch (Exception ex)
                    {
                       tcs1.TrySetResult(new AuthenticatorCompletedEventArgs(null));
                    }
                };

            try
            {
                auth.Completed += d1;
                var vc = auth.GetUI();
                dialog.PresentViewController(vc, true, null);
                var result= await tcs1.Task;
                return result.Account;
            }
            catch (Exception)
            {
               // todo handle the exception
                return null;
            }
            finally
            {
                auth.Completed -= d1;
            }
        }

And then we need to call it this way:

var authService = new AuthService();
var result = await authService.LoginAsync(dialog, allowCancel);

In conclusion, i think it is very simple to use and i think it should be add to the Xamarin.Auth.

I added the sample to:
https://github.com/saramgsilva/Xamarin.Auth/tree/master/samples

and will do a pull request for the original repository.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

saramgsilva
Software Developer (Senior)
Portugal Portugal
I am Sara Silva, i studied mathematics in University of Coimbra, but since i finish my degree I’m working in software development using Microsoft technologies.
 
Nowadays I am a Windows 8 Store, Windows Phone, Xamarin and Azure Developer in Portugal, with experience in the development of projects within the operational planning of public transport, logistics, payments and multimedia.
 
I was awarded
 
Telerik Developer Expert
Microsoft Most Valuable Professional (MVP) 2015 – .Net
Microsoft Most Valuable Professional (MVP) 2014 – Visual C#
Microsoft Most Valuable Professional (MVP) 2013 – Visual C#
C# Corner Most Valuable Professional (MVP)
Nokia Developer Champion
 

Others awards:
 
Nokia Developer Wiki Contributor of the Month for March 2013
Nokia Developer Wiki Contributor of the Month for November 2013
Nokia Developer Wiki Contributor of the Month for August 2014
TechNet Wiki – Longest Article Award – May, September 2014
TechNet Wiki – Windows Phone and Windows Store Apps Technical Guru – May, June, July, August, September, October 2014
TechNet Wiki – Microsoft Azure Technical Guru – July, September, October 2014
TechNet Wiki – Visual C# Technical Guru – August, October 2014
TechNet Wiki – Miscellaneous Technical Guru – August, October 2014
TechNet Wiki – WPF Technical Guru – October, November 2014
Curah! 2014 Award – Top 10 featured curators
 

My profissional profile:
 
Windows 8 Store Apps Developer
Windows Phone Developer
WPF Developer
Xamarin Developer
Azure Developer
 

My Certifications:
 
MCTS: .NET Framework 4.0, Windows Applications
MCPD: Windows Developer 3.5
MCTS: .NET Framework 3.5, Windows Forms Applications
MCTS: .NET Framework 3.5 Windows Presentation Foundation Applications
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
QuestionLogin by google account PinprofessionalMember-99638345-Sep-14 3:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150129.1 | Last Updated 30 Jun 2014
Article Copyright 2014 by saramgsilva
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid