SHARE
TWEET

Untitled

a guest Aug 13th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class DroidOAuth2Authenticator : OAuth2Authenticator
  2.     {
  3.         public DroidOAuth2Authenticator(string clientId, string scope, Uri authorizeUrl, Uri redirectUrl) : base(clientId, scope, authorizeUrl, redirectUrl)
  4.         {
  5.  
  6.         }
  7.  
  8.         protected override void OnPageEncountered(Uri url, System.Collections.Generic.IDictionary<string, string> query, System.Collections.Generic.IDictionary<string, string> fragment)
  9.         {
  10.             // Remove state from dictionaries.
  11.             // We are ignoring request state forgery status
  12.             // as we're hitting an ASP.NET service which forwards
  13.             // to a third-party OAuth service itself
  14.             if (query.ContainsKey("state"))
  15.             {
  16.                 query.Remove("state");
  17.             }
  18.  
  19.             if (fragment.ContainsKey("state"))
  20.             {
  21.                 fragment.Remove("state");
  22.             }
  23.  
  24.             base.OnPageEncountered(url, query, fragment);
  25.         }
  26.     }
  27.  
  28.     public class OAuth2Service
  29.     {
  30.         public event EventHandler<string> OnSuccess = delegate { };
  31.         public event EventHandler OnCancel = delegate { };
  32.         public event EventHandler<string> OnError = delegate { };
  33.  
  34.         public void Authenticate(string clientId, string scope, Uri authorizeUrl, Uri redirectUrl)
  35.         {
  36.             var activity = CrossCurrentActivity.Current.Activity;
  37.  
  38.             var auth = new DroidOAuth2Authenticator(
  39.                 clientId: clientId, // your OAuth2 client id
  40.                 scope: scope, // the scopes for the particular API you're accessing, delimited by "+" symbols
  41.                 authorizeUrl: authorizeUrl, // the auth URL for the service
  42.                 redirectUrl: redirectUrl); // the redirect URL for the service
  43.  
  44.             auth.AllowCancel = true;
  45.             auth.ShowErrors = false;
  46.  
  47.             EventHandler<AuthenticatorErrorEventArgs> errorDelegate = null;
  48.             EventHandler<AuthenticatorCompletedEventArgs> completedDelegate = null;
  49.  
  50.             errorDelegate = (sender, eventArgs) =>
  51.             {
  52.                 OnError?.Invoke(this, eventArgs.Message);
  53.  
  54.                 auth.Error -= errorDelegate;
  55.                 auth.Completed -= completedDelegate;
  56.             };
  57.  
  58.             completedDelegate = (sender, eventArgs) => {
  59.  
  60.                 // UI presented, so it's up to us to dimiss it on Android
  61.                 // dismiss Activity with WebView or CustomTabs
  62.                 CrossCurrentActivity.Current.Activity.Finish();
  63.  
  64.                 if (eventArgs.IsAuthenticated)
  65.                 {
  66.  
  67.                     OnSuccess?.Invoke(this, eventArgs.Account.Properties["access_token"]);
  68.  
  69.                 }
  70.                 else
  71.                 {
  72.                     // The user cancelled
  73.  
  74.                     OnCancel?.Invoke(this, EventArgs.Empty);
  75.                 }
  76.                 auth.Error -= errorDelegate;
  77.                 auth.Completed -= completedDelegate;
  78.  
  79.             };
  80.  
  81.             auth.Error += errorDelegate;
  82.             auth.Completed += completedDelegate;
  83.  
  84.             activity.StartActivity(auth.GetUI(activity));
  85.         }
  86.   }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top