android - Xamarin Forms 项目中使用 AD 和 Google 进行的 Azure 身份验证在授权后不会重定向回应用程序

标签 android azure authentication xamarin.forms azure-mobile-services

Azure Activity 目录

Google+ 身份验证

Xamarin 表单、PCL (NuGet 2.4.0.282)

Microsoft.Azure.Mobile.Client 4.0.0 和 4.0.2

成功登录后,我的手机没有返回到我的应用程序。我有两部测试手机和一部模拟器,登录后它们显示不同的信息。

电话 1(AAD 验证): enter image description here

电话 1(Google Auth 呈灰色显示并保持“正在加载”) enter image description here

电话 2(AAD 和 Google Auth): enter image description here

模拟器(AAD 和 Google Auth): enter image description here

我已经完成了在 Stack OverFlow 上找到的所有内容,这很有意义,并且似乎适用于当前版本的 NuGets。 此人似乎与我有类似的问题,但需要使用 Google 登录 Azure not redirecting after login enter link description here

我尝试将代码集成到我的项目中。然后我将我的 Azure 信息输入到 Xamarin 的示例中:https://github.com/xamarin/xamarin-forms-samples/tree/master/WebServices/TodoAzureAuth

我得到了相同的结果。我已经尝试过 AAD 和 Google+ Auth。登录后,它仅停留在浏览器中。所以我觉得客户端代码必须是正确的。但我在我的 Azure 服务器代码上找不到任何困惑的地方。我已经在具有 C# 和 Node.Js 后端的项目中尝试过此操作。(对于我的一个项目)我允许的外部重定向 URL 是 ToDoList53172://easyauth.callback,在我的 AndroidManifest.xml 中如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.xamarin.sample.TodoAzure">
    <uses-sdk android:minSdkVersion="15" />
    <application android:label="TodoAzure" android:icon="@drawable/icon">
        <activity android:name="com.microsoft.windowsazure.mobileservices.authentication.RedirectUrlActivity" android:launchMode="singleTop" android:noHistory="true">
            <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:scheme="ToDoList53172" android:host="easyauth.callback" />
            </intent-filter>
        </activity>
    </application>
</manifest>

旧: 而且我觉得我不应该发布所有其他代码。这一切都在上面发布的 Xamarin 示例项目中。如果人们认为我应该这样做,我就会这样做。 新的: 我添加更多代码只是为了帮助人们。我不想重载,但最好将所有信息集中在一处。这是我的 MainActivity.cs 代码

using System;
using System.Threading.Tasks;
using Android.App;
using Android.Content.PM;
using Android.OS;
using Microsoft.WindowsAzure.MobileServices;
using Android.Webkit;

namespace TodoAzure.Droid
{
    [Activity(Label = "TodoAzure.Droid",
        Icon = "@drawable/icon",
        MainLauncher = true,
        ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,
        Theme = "@android:style/Theme.Holo.Light")]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity, IAuthenticate
    {
        MobileServiceUser user;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            global::Xamarin.Forms.Forms.Init(this, bundle);
            Microsoft.WindowsAzure.MobileServices.CurrentPlatform.Init();
            App.Init((IAuthenticate)this);
            LoadApplication(new App());
        }

        public async Task<bool> AuthenticateAsync()
        {
            bool success = false;
            try
            {
                if (user == null)
                {
                    // The authentication provider could also be Facebook, Twitter, or Microsoft
                    user = await TodoItemManager.DefaultManager.CurrentClient.LoginAsync(this, MobileServiceAuthenticationProvider.Google, Constants.URLScheme);
                    if (user != null)
                    {
                        CreateAndShowDialog(string.Format("You are now logged in - {0}", user.UserId), "Logged in!");
                    }
                }
                success = true;
            }
            catch (Exception ex)
            {
                CreateAndShowDialog(ex.Message, "Authentication failed");
            }
            return success;
        }

        public async Task<bool> LogoutAsync()
        {
            bool success = false;
            try
            {
                if (user != null)
                {
                    CookieManager.Instance.RemoveAllCookie();
                    await TodoItemManager.DefaultManager.CurrentClient.LogoutAsync();
                    CreateAndShowDialog(string.Format("You are now logged out - {0}", user.UserId), "Logged out!");
                }
                user = null;
                success = true;
            }
            catch (Exception ex)
            {
                CreateAndShowDialog(ex.Message, "Logout failed");
            }

            return success;
        }

        void CreateAndShowDialog(string message, string title)
        {
            var builder = new AlertDialog.Builder(this);
            builder.SetMessage(message);
            builder.SetTitle(title);
            builder.SetNeutralButton("OK", (sender, args) => { });
            builder.Create().Show();
        }
    }
}

就像我上面所说的,我也用 AAD 尝试过这一点。上面的代码适用于 Google。

这是我的 Azure 身份验证设置 enter image description here

这是我使用“https://todolistjbb.azurewebsites.net/.auth/login/aad”登录然后访问后得到的信息 “https://todolistjbb.azurewebsites.net/.auth/meenter image description here

我觉得我已经尝试了很多东西。我记录了 66.68 小时的工作时间,只是试图在我的应用程序中获得身份验证...请...有人告诉我我做错了什么!我在这里迷失了:'(

最佳答案

解决此问题的方法是不要以大写字母开头的 URL 方案。我花了两个多星期才弄清楚。我不认为这个姐姐在任何地方写过,但我确信它是。 所以是的,为了解决这个问题,我将“ToDoList53172”切换为“todolist53172” 就是这样...哎哟!

关于android - Xamarin Forms 项目中使用 AD 和 Google 进行的 Azure 身份验证在授权后不会重定向回应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48490540/

相关文章:

azure - 如何通过 Python API 从 azure 数据湖高效下载整个目录?

azure - 从应用程序更改连接字符串中的数据库

带有重定向的python请求

java - 当viewpager重新启动时,它会继续执行之前的fragment

android - 添加一个 AnimationDrawable 到 BottomNavigationView

隐藏在底部栏下方的Android float 操作栏

java - 按钮点击错误

Azure Web 应用服务支持 Python websocket?

authentication - 以 Google Apps super 管理员身份通过 API 为用户关闭两步验证

node.js - 使用@octokit 对 Github 应用程序进行身份验证