Xamarin.Auth (Android) - Chrome custom tabs not closing on redirect

I have implemented the Xamarin.Auth sample code to authenticate with the Google Identity Provider on Android. I have successfully navigated to the google login page using the device browser browser where I can enter my credentials. I successfully log in with google but the custom chrome tabs do not close when redirecting back to my app, meaning I stay on google search in chrome browser. If I close my browser, I can see my app again with user data from google credential provider.

Why won't Chrome custom tabs close when redirected from the Google Id Provider, and how can I close it using Xamarin Forms and Xamarin.Auth?

+3


source to share


1 answer


You can get back to your application if you add this code to the end of the OnCreate method in the class that grabs the Redirect (CustomUrlSchemeInterceptorActivity) in the Xamarin.Auth example in Android

new Task(() =>{
         StartActivity(new Intent(Application.Context,typeof(MainActivity)));
     }).Start();

      

Where MainActivity is the name of your main activity class in Android. More precisely, it is a complete class that you can inherit for every redirect you intercept.



public class UrlSchemeInterceptor : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        try
        {
            base.OnCreate(savedInstanceState);

            // Convert Android.Net.Url to Uri
            var uri = new Uri(Intent.Data.ToString());
            new Task(() =>
            {
                var intent = new Intent(ApplicationContext, typeof(MainActivity));
                intent.AddFlags(ActivityFlags.IncludeStoppedPackages);
                intent.AddFlags(ActivityFlags.ReorderToFront);
                StartActivity(intent);

            }).Start();

            // Load redirectUrl page
            AuthenticationState.Authenticator.OnPageLoading(uri);
            Finish();

        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
    }

}

public class AuthenticationState
{
    public static WebAuthenticator Authenticator;
   /*This static field is used to store the object
   from OAuth1Authenticator or OAuth2Authenticator
   upon initialization in the UI (Xamarin forms or Android or iOS).
   For example:
   var authenticatorObject = new  OAuth2Authenticator (YOUR PARAMETERS);
   AuthenticationState.Authenticator = (WebAuthenticator)authenticatorObject;
    var presenter = new OAuthLoginPresenter();
        presenter.Login(authenticatorObject);
    */
}

      

For example, in Google Case

[Activity(Label = "YOURLABEL", NoHistory = true, LaunchMode = LaunchMode.SingleTop)]
[IntentFilter( new[] { Intent.ActionView }, Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
    DataSchemes = new[]
    {
        "com.googleusercontent.apps.",// YOUR GOOGLE ID INVERTED
    },
    DataPaths = new[]
    {
        "/oauth2redirect",
    })]
public class GoogleUrlSchemeInterceptorActivity : UrlSchemeInterceptor { }

      

+5


source







All Articles