代码之家  ›  专栏  ›  技术社区  ›  pavel

沙马林。android+auth0身份验证错误:net:ERR\u UNKNOWN\u URL\u SCHEME

  •  0
  • pavel  · 技术社区  · 6 年前

    我想登录我的xamarin。通过auth0的android应用程序。我发现这个教程描述得很好。

    我只是用xamarin。android改为c#上的“纯”android应用程序。

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

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

    这是我的主要活动代码:

    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 回复  |  直到 6 年前
        1
  •  0
  •   pavel    6 年前

    好吧,我刚刚解决了。

    我忘了指出那个意图。类别故障,意图。IntentFilter上的类别可浏览类别:

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

    我希望这能为某人节省一些时间。