【问题标题】:Xamarin.Auth (Android) - Chrome Custom Tabs Doesn't Close on RedirectXamarin.Auth (Android) - Chrome 自定义选项卡不会在重定向时关闭
【发布时间】:2017-07-27 12:33:29
【问题描述】:

我已实现 Xamarin.Auth sample code 以在 Android 上通过 google 的身份提供程序进行身份验证。我已使用设备的 Chrome 浏览器成功导航到 Google 登录页面,我可以在其中输入我的凭据。我已成功通过 Google 授权,但 Chrome 自定义选项卡在重定向回我的应用程序时并未关闭,即我只能在 chrome 浏览器中查看 google 搜索。如果我关闭浏览器,我可以再次看到我的应用,并显示从谷歌身份提供者返回的用户详细信息。

为什么 Chrome 的自定义选项卡不会在从 Google 身份提供程序重定向时关闭,我如何使用 Xamarin Forms 和 Xamarin.Auth 关闭它?

【问题讨论】:

标签: android google-chrome xamarin.forms xamarin.auth


【解决方案1】:

如果您将此代码添加到捕获 Android 中 Xamarin.Auth 示例中的重定向 (CustomUrlSchemeInterceptorActivity) 的类中的 OnCreate 方法的末尾,您可以返回您的应用

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

其中 MainActivity 是您在 Android 中的主要 Activity 类的名称。 更准确地说,这里有一个完整的类,您可以为拦截的每个重定向继承它

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);
    */
}

例如在谷歌案例中

[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 { }

【讨论】:

  • 对我不起作用。 public class GoogleAuthInterceptor : Activity { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); Android.Net.Uri uri_android = Intent.Data; Uri uri_netfx = new Uri(uri_android.ToString()); MainActivity.Auth?.OnPageLoading(uri_netfx); Finish(); new Task(() => { StartActivity(new Intent(Application.Context, typeof(MainActivity))); }).Start(); } }
  • 我进一步阐明了这个例子,并从一个对我有用的项目中添加了代码。
  • 嗨,艾哈迈德,不。仍然不适合我。根据您的建议,我实现了完全相同的代码。但是,我找不到任何实现 AuthenticationState 的库,因此我将其替换为以下内容:而不是这个:AuthenticationState.Authenticator.OnPageLoading(uri); 我实现了这个:MainActivity.Auth?.OnPageLoading(uri);
  • 是的。如果您说 Auth 是一个包含验证器对象的静态对象,那么它应该可以工作。无论如何,我添加了缺失的部分。享受吧。
猜你喜欢
  • 2017-09-05
  • 1970-01-01
  • 1970-01-01
  • 2021-01-21
  • 1970-01-01
  • 2017-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多