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 libraries that don´t use the async/await and for me it is fundamental.

One library that I think should use asyn/await but that I saw does not use it is Xamarin.Auth!

For uisng Xamarin.Auth.OAuth2Authenticator for authentication I recommend the following solution.

For Xamarin.Android

  1. public async Task<Account> LoginAsync(Activity activity, bool allowCancel)  
  2. {  
  3.      var auth = new OAuth2Authenticator(  
  4.      clientId: "<scopes here>",  
  5.      scope: "<scopes here>",  
  6.      authorizeUrl: new Uri("<url here>"),  
  7.      redirectUrl: new Uri("<url here>"))  
  8.      {  
  9.          AllowCancel = allowCancel  
  10.      };  
  11.      // If authorization succeeds or is canceled, .Completed will be fired.  
  12.      var tcs1 = new TaskCompletionSource<AuthenticatorCompletedEventArgs>();  
  13.      EventHandler<AuthenticatorCompletedEventArgs> d1 =  
  14.      (o, e) =>  
  15.      {  
  16.          try  
  17.          {  
  18.              tcs1.TrySetResult(e);  
  19.          }  
  20.          catch (Exception ex)  
  21.          {  
  22.              tcs1.TrySetResult(new AuthenticatorCompletedEventArgs(null));  
  23.          }  
  24.      };  
  25.      try  
  26.      {  
  27.          auth.Completed += d1;  
  28.          var intent = auth.GetUI(activity);  
  29.          activity.StartActivity(intent);  
  30.          var result= await tcs1.Task;  
  31.          return result.Account;  
  32.      }  
  33.      catch (Exception)  
  34.      {  
  35.          // todo you should handle the exception  
  36.          return null;  
  37.      }  
  38.      finally  
  39.      {  
  40.          auth.Completed -= d1;  
  41.      }  
  42. }  
And then we need to call it this way:
  1. var authService = new AuthService();  
  2. var result = await authService.LoginAsync(this, allowCancel);  
For Xamarin.IOS
  1. public async Task<Account> LoginAsync(DialogViewController dialog, bool allowCancel)  
  2. {  
  3.      var auth = new OAuth2Authenticator(  
  4.      clientId: "<scopes here>",  
  5.      scope: "<scopes here>",  
  6.      authorizeUrl: new Uri("<url here>"),  
  7.      redirectUrl: new Uri("<url here>"))  
  8.      {  
  9.           AllowCancel = allowCancel  
  10.      };  
  11.    
  12.      // If authorization succeeds or is canceled, .Completed will be fired.  
  13.      var tcs1 = new TaskCompletionSource<AuthenticatorCompletedEventArgs>();  
  14.      EventHandler<AuthenticatorCompletedEventArgs> d1 =  
  15.      (o, e) =>  
  16.      {  
  17.           try  
  18.           {  
  19.               tcs1.TrySetResult(e);  
  20.           }  
  21.           catch (Exception ex)  
  22.           {  
  23.               tcs1.TrySetResult(new AuthenticatorCompletedEventArgs(null));  
  24.           }  
  25.      };  
  26.      try  
  27.      {  
  28.           auth.Completed += d1;  
  29.           var vc = auth.GetUI();  
  30.           dialog.PresentViewController(vc, truenull);  
  31.           var result= await tcs1.Task;  
  32.           return result.Account;  
  33.      }  
  34.      catch (Exception)  
  35.      {  
  36.           // todo handle the exception  
  37.           return null;  
  38.      }  
  39.      finally  
  40.      {  
  41.           auth.Completed -= d1;  
  42.      }  
  43. }  
And then we need to call it this way:
  1. var authService = new AuthService();  
  2. var result = await authService.LoginAsync(dialog, allowCancel);  
In conclusion, I think it is very simple to use and I think it should be added 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.


Similar Articles