【问题标题】:Xamarin forms Android OAuth2 redirect no workingXamarin 形成 Android OAuth2 重定向不起作用
【发布时间】:2019-12-16 13:40:47
【问题描述】:

所以,我已经为此工作了几天,但我错过了一些东西。我正在尝试创建一个Xamarin 表单应用程序,该应用程序使用OAuth2 身份验证连接到云会计包。我一直在研究here 中的示例。但我就是无法让重定向工作。很可能是一些简单的事情,但我没有看到或理解

我根据示例创建了身份验证器,将重定向 url 设置为 https://com.myproject.myapp/oauth2redirect

var authenticator = new OAuth2Authenticator(
            <ClientId>,
            null,
            "openid profile email accounting.transactions accounting.settings offline_access",
            new <AuthoriseationEndpoint>,
            new Uri("https://com.myproject.myapp/oauth2redirect"),
            new <TokenEndpoint>,
            null,
            true);

然后,我在我的 Android 项目中使用 intentFilter 创建了一个新的 activity

    [Activity(Label = "CustomUrlSchemeInterceptorActivity", NoHistory = true)]
    [IntentFilter(new[] { Android.Content.Intent.ActionView },
        Categories = new[] { Android.Content.Intent.CategoryDefault, Android.Content.Intent.CategoryBrowsable },
        DataSchemes = new[] { "com.myproject.myapp" },
        DataPath = "/oauth2redirect")]
    public class CustomUrlSchemeInterceptorActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            var uri = new Uri(Intent.Data.ToString());

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

            Finish();

            // Create your application here
        }
    }

当我运行我的应用程序时,登录屏幕会按预期弹出。我的应用程序在云应用程序中注册正常,但重定向不起作用。我只是收到一个浏览器错误,说无法访问该站点。 Server找不到IP地址DNS_PROBE_FINISHED_NXDOMAIN

【问题讨论】:

    标签: c# android xamarin.forms oauth-2.0


    【解决方案1】:

    尝试改变

    var authenticator = new OAuth2Authenticator(
            <ClientId>,
            null,
            "openid profile email accounting.transactions accounting.settings offline_access",
            new <AuthoriseationEndpoint>,
            new Uri("https://com.myproject.myapp/oauth2redirect"),
            new <TokenEndpoint>,
            null,
            true);
    

    var authenticator = new OAuth2Authenticator(
            <ClientId>,
            null,
            "openid profile email accounting.transactions accounting.settings offline_access",
            new <AuthoriseationEndpoint>,
            new Uri("com.myproject.myapp:/oauth2redirect"),
            new <TokenEndpoint>,
            null,
            true);
    

    定义方案后,您无法将 https:// 添加到自定义重定向 URL。请注意,在方案组件之后只能出现一个/。 你可以参考Redirect URL

    【讨论】:

    • 谢谢,确实尝试过,因为我读到了单曲 /。但是,如果 https:// 不存在,我会从 oAuth 服务器作为 sson as presenter.login 行收到错误。我假设这是因为云服务中的重定向 url 不匹配,但这不会让我使用没有 https 的 uri
    • @Hursey 你的意思是你在OAuth服务网站上定义了重定向urlhttps://com.myproject.myapp/oauth2redirect
    • 朱正是。他们需要完整的 Url 才能进行回调。
    • @Hursey 我不太明白你的意思。也许你可以看看这个Xamarin.Auth
    • 感兴趣的Ta。似乎我并不孤单遇到这个问题。一直在与其他几个开发人员讨论。目前看来,OAuth2 进程云计费包不支持这种类型的应用程序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多