简体   繁体   English

Xamarin.Auth(Android) - Chrome自定义标签在重定向时不会关闭

[英]Xamarin.Auth (Android) - Chrome Custom Tabs Doesn't Close on Redirect

I've implemented the Xamarin.Auth sample code to authenticate with google's identity provider on Android. 我已经实现了Xamarin.Auth 示例代码,以便在Android上使用google的身份提供程序进行身份验证。 I'm successfully navigated to the Google login page using the device's Chrome browser where I can enter my credentials. 我已成功使用设备的Chrome浏览器导航到Google登录页面,我可以在其中输入凭据。 I successfully authorize with Google but the Chrome Custom Tabs doesn't close when it redirects back to my app, ie, I'm left looking at the google search in the chrome browser. 我已成功向Google授权,但Chrome自定义标签在重定向回我的应用时并未关闭,即我在Chrome浏览器中查看谷歌搜索。 If I close the browser I can see my app again with the user details returned from google's identity provider displayed. 如果我关闭浏览器,我会再次看到我的应用,并显示从谷歌身份提供商返回的用户详细信息。

Why is Chrome's custom tabs not closing on redirect from the Google identity provider, and how can I get it to close using Xamarin Forms and Xamarin.Auth? 为什么Chrome的自定义标签不会从Google身份提供商重定向关闭,如何使用Xamarin Forms和Xamarin.Auth关闭它?

You can go back to your app if you add this code to the end of OnCreate method in the class that captures the Redirect (CustomUrlSchemeInterceptorActivity) in Xamarin.Auth example in Android 如果您将此代码添加到捕获Android中Xamarin.Auth示例中的Redirect(CustomUrlSchemeInterceptorActivity)的类中的OnCreate方法的末尾,则可以返回到您的应用程序

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

Where MainActivity is the name of your main Activity class in Android. 其中MainActivity是Android中主要Activity类的名称。 To be more exact here is a complete class that you can inherit for each redirection 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 { }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM