Xamarin.android + auth0身份验证错误:net:ERR_UNKNOWN_URL_SCHEME

时间:2018-04-28 13:30:55

标签: android xamarin xamarin.android auth0

我想通过auth0登录我的xamarin.android应用程序。我发现本教程描述它非常好。

我只使用xamarin.android而不是#34; pure" c#上的android app。

但我有这个错误:  enter image description here

如您所见,我通过callback?code={code}获得了auth0的有效回复 但似乎意图激活方法存在一些问题。 (它无法在正确的方案中解析响应。也许,它假设它的网址是通过浏览器进行重定向调用)。

有我的MainActivity代码:

usings ***

namespace myApp.intake.Mobile.Droid
{
    [Activity(Label = "App", 
        Icon = "@drawable/icon", 
        Theme = "@style/MainTheme", 
        MainLauncher = true,
        LaunchMode = LaunchMode.SingleInstance,
        ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    [IntentFilter(
        new[] { Intent.ActionView },
        DataScheme = "myApp.intake.mobile",
        DataHost = "myApp-dev.auth0.com",
        DataPathPrefix = "/android/myApp.intake.mobile/callback")]
    public class MainActivity : FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(bundle);

            client = new Auth0Client(new Auth0ClientOptions
            {
                Domain = "myApp-dev.auth0.com",
                ClientId = "************", //real clientId
                Activity = this
            });

            Forms.Init(this, bundle);
            LoadApplication(new App());

            this.PerformAuth0Login();
        }

        protected override async void OnNewIntent(Intent intent)
        {
            // I have to get redirected here but it just throw the mentioned error!!!!!!
            base.OnNewIntent(intent);

            var loginResult = await client.ProcessResponseAsync(intent.DataString, authorizeState);

            var sb = new StringBuilder();
            if (loginResult.IsError)
            {
                // logic to handle the error
            }
            // next business logic
        }

        #region Private Logic

        private async void PerformAuth0Login()
        {
            // Prepare for the login
            authorizeState = await client.PrepareLoginAsync();

            // Send the user off to the authorization endpoint
            var uri = Android.Net.Uri.Parse(authorizeState.StartUrl);
            var intent = new Intent(Intent.ActionView, uri);
            intent.AddFlags(ActivityFlags.NoHistory);
            StartActivity(intent); //I think problem here..
        }

        #endregion

        #region Private Fields

        private Auth0Client client;
        private AuthorizeState authorizeState;

        #endregion
    }
}

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

好的,我刚解决了。

我忘了在IntentFilter上指出Intent.CategoryDe​​fault,Intent.CategoryBrowsable类别:

[Activity(...)]
[IntentFilter(
        new[] { Intent.ActionView },
        Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable },
        DataScheme = "myApp.intake.mobile",
        DataHost = "myApp-dev.auth0.com",
        DataPathPrefix = "/android/myApp.intake.mobile/callback")]
    public class MainActivity : FormsAppCompatActivity
{...}

据我所知,这个属性必须编译成AndroidManifest.xml文件,如下所示:

  <activity
    android:name="com.spotify.sdk.demo.DemoActivity"
    android:label="IntakeMobile"
    android:launchMode="singleInstance">
    <intent-filter>
      <action android:name="android.intent.action.VIEW"/>
      <category android:name="android.intent.category.DEFAULT"/>
      <category android:name="android.intent.category.BROWSABLE"/>
      <data
         android:host="callback"
         android:scheme="testschema"/>
    </intent-filter>
  </activity>

我希望它可以节省一些时间。