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. // 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 vc = auth.GetUI();
  29. dialog.PresentViewController(vc, true, null);
  30. var result= await tcs1.Task;
  31. return result.Account;
  32. }
  33. catch (Exception)
  34. {
  35. // todo 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(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.