/// <summary> /// Opens the system web browser for the user to sign in to their account. /// </summary> /// <param name="customUrlHandler"> /// A handler that will open a URL in system browser, register a custom /// callback URL handler and wait for the app to go foreground. /// /// You can use <see cref="SafariUrlHandler.Instace" />, given that you call its /// <c>WillEnterForeground</c> and <c>HandleOpenUrl</c> methods from your <c>AppDelegate</c>. /// </param> /// <returns> /// A task for when authentication has completed. /// </returns> public virtual Task <IEnumerable <Account> > GetAccountsWithBrowserAsync(ICustomUrlHandler customUrlHandler) { if (customUrlHandler == null) { throw new ArgumentNullException("customUrlHandler", "This overload needs a handler to launch system browser and wait for redirect. " + "You can use SafariUrlHandler.Instace, given that you call its " + "WillEnterForeground and HandleOpenUrl methods from your AppDelegate."); } var tcs = new TaskCompletionSource <IEnumerable <Account> > (); var authenticator = GetAuthenticator() as WebAuthenticator; if (authenticator == null) { throw new NotSupportedException("This service does not support authentication via web browser."); } authenticator.Error += (sender, e) => { tcs.TrySetException(e.Exception ?? new SocialException(e.Message)); }; authenticator.Completed += (sender, e) => { if (e.IsAuthenticated) { SaveAccount(e.Account); tcs.TrySetResult(new [] { e.Account }); } else { tcs.TrySetCanceled(); } }; authenticator.AuthenticateWithBrowser(customUrlHandler); return(tcs.Task); }
/// <summary> /// Opens authentication page in system browser and waits for the app to go foreground with a custom URL. /// </summary> /// <param name="handler">Custom URL handler. Use <see cref="SafariUrlHandler.Instance"/> for iOS.</param> public void AuthenticateWithBrowser(ICustomUrlHandler handler) { if (CustomUrl.AbsoluteUri.StartsWith("http")) { throw new InvalidOperationException(string.Format( "You need to replace '{0}' with application-specific custom URL to use browser authentication.", CustomUrl)); } GetInitialUrlAsync().ContinueWith(initUrlTask => { if (initUrlTask.IsFaulted) { OnError(initUrlTask.Exception); } else if (initUrlTask.IsCanceled) { OnCancelled(); } var externalUrl = initUrlTask.Result; handler.OpenUrl(externalUrl, CustomUrl.Scheme).ContinueWith(callbackTask => { if (callbackTask.IsFaulted) { OnError(callbackTask.Exception); } else if (callbackTask.IsCanceled) { OnCancelled(); } else { OnPageLoaded(callbackTask.Result); } }, TaskScheduler.FromCurrentSynchronizationContext()); }, TaskScheduler.FromCurrentSynchronizationContext()); }
/// <summary> /// Opens authentication page in system browser and waits for the app to go foreground with a custom URL. /// </summary> /// <param name="handler">Custom URL handler. Use <see cref="SafariUrlHandler.Instance"/> for iOS.</param> public void AuthenticateWithBrowser (ICustomUrlHandler handler) { if (CustomUrl.AbsoluteUri.StartsWith ("http")) throw new InvalidOperationException (string.Format ( "You need to replace '{0}' with application-specific custom URL to use browser authentication.", CustomUrl)); GetInitialUrlAsync ().ContinueWith (initUrlTask => { if (initUrlTask.IsFaulted) OnError (initUrlTask.Exception); else if (initUrlTask.IsCanceled) OnCancelled (); var externalUrl = initUrlTask.Result; handler.OpenUrl (externalUrl, CustomUrl.Scheme).ContinueWith (callbackTask => { if (callbackTask.IsFaulted) OnError (callbackTask.Exception); else if (callbackTask.IsCanceled) OnCancelled (); else OnPageLoaded (callbackTask.Result); }, TaskScheduler.FromCurrentSynchronizationContext ()); }, TaskScheduler.FromCurrentSynchronizationContext ()); }
/// <summary> /// Opens the system web browser for the user to sign in to their account. /// </summary> /// <param name="customUrlHandler"> /// A handler that will open a URL in system browser, register a custom /// callback URL handler and wait for the app to go foreground. /// /// You can use <see cref="SafariUrlHandler.Instace" />, given that you call its /// <c>WillEnterForeground</c> and <c>HandleOpenUrl</c> methods from your <c>AppDelegate</c>. /// </param> /// <returns> /// A task for when authentication has completed. /// </returns> public virtual Task<IEnumerable<Account>> GetAccountsWithBrowserAsync (ICustomUrlHandler customUrlHandler) { if (customUrlHandler == null) throw new ArgumentNullException ("customUrlHandler", "This overload needs a handler to launch system browser and wait for redirect. " + "You can use SafariUrlHandler.Instace, given that you call its " + "WillEnterForeground and HandleOpenUrl methods from your AppDelegate."); var tcs = new TaskCompletionSource<IEnumerable<Account>> (); var authenticator = GetAuthenticator () as WebAuthenticator; if (authenticator == null) throw new NotSupportedException ("This service does not support authentication via web browser."); authenticator.Error += (sender, e) => { tcs.TrySetException (e.Exception ?? new SocialException (e.Message)); }; authenticator.Completed += (sender, e) => { if (e.IsAuthenticated) { SaveAccount (e.Account); tcs.TrySetResult (new [] { e.Account }); } else { tcs.TrySetCanceled (); } }; authenticator.AuthenticateWithBrowser (customUrlHandler); return tcs.Task; }