简体   繁体   English

授权后,Xamarin Forms项目中具有AAD和Google的Azure身份验证不会重定向回应用程序

[英]Azure Authentication with AAD & Google in a Xamarin Forms Project not Redirecting back to app after Authorized

Azure Active Directory Azure活动目录

Google+ Auth Google+身份验证

Xamarin Forms, PCL (NuGet 2.4.0.282) Xamarin Forms,PCL(NuGet 2.4.0.282)

Microsoft.Azure.Mobile.Client 4.0.0 & 4.0.2 Microsoft.Azure.Mobile.Client 4.0.0和4.0.2

After I successfully Login my phone does not return to my app. 成功登录后,我的手机不会返回我的应用程序。 I have two test phones and one emulator, they display different info, after login. 我有两台测试电话和一个模拟器,登录后它们显示不同的信息。

Phone 1 (AAD Auth): 电话1(AAD身份验证): 在此处输入图片说明

Phone 1 (Google Auth it greys out and just keeps "loading") 电话1(Google身份验证显示为灰色,仅保持“正在加载”) 在此处输入图片说明

Phone 2 (AAD and Google Auth): 电话2(AAD和Google Auth): 在此处输入图片说明

Emulator (AAD and Google Auth): 模拟器(AAD和Google Auth): 在此处输入图片说明

I have done everything I found here on Stack OverFlow, that makes sense and seems to be applicable to current versions of NuGets. 我已经完成了我在Stack OverFlow上找到的所有内容,这很有意义,而且似乎适用于当前版本的NuGets。 This person seems to be having a similar issue to me but with Google Log in Azure not redirecting after login enter link description here 这个人似乎和我有类似的问题,但是在登录后Azure中的 Google Log 无法重定向,请在 此处输入链接描述

I have tried integrating code into my project. 我尝试将代码集成到我的项目中。 And then I input my Azure info into Xamarin's sample: https://github.com/xamarin/xamarin-forms-samples/tree/master/WebServices/TodoAzureAuth 然后我将我的Azure信息输入Xamarin的示例中: https : //github.com/xamarin/xamarin-forms-samples/tree/master/WebServices/TodoAzureAuth

And I get the same results. 我得到相同的结果。 I have tried both AAD and Google+ Auth. 我已经尝试过AAD和Google+ Auth。 After login it just stays at the browser. 登录后,它仅停留在浏览器中。 So I feel like the client side code has to be correct. 所以我觉得客户端代码必须正确。 But I cant find any mess up on my Azure server code. 但是我在我的Azure服务器代码上找不到任何混乱。 I have tried this with projects that have a C# and Node.Js backend.(For one of my projects) My ALLOWED EXTERNAL REDIRECT URLS is ToDoList53172://easyauth.callback and in my AndroidManifest.xml looks like this: 我已经在具有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>

OLD: And I don't feel like I should post all the other code. OLD:我觉得我不应该发布所有其他代码。 It is all in the Xamarin sample project posted above. 全部都在上面发布的Xamarin示例项目中。 If people think I should I will. 如果人们认为我应该。 NEW: I am adding more code just to help people out. 新:我添加了更多代码只是为了帮助人们。 I did not want to overload, but better to have all the info in one place. 我不想超载,但最好将所有信息放在一个地方。 So here is my MainActivity.cs Code 这是我的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();
        }
    }
}

And Like I said above I have tried this with AAD as well. 就像我上面说的,我也曾在AAD上尝试过。 The code above is for Google. 上面的代码适用于Google。

Here is my Azure Auth setup 这是我的Azure身份验证设置 在此处输入图片说明

Here is the info I get after logging in with " https://todolistjbb.azurewebsites.net/.auth/login/aad " and then visiting " https://todolistjbb.azurewebsites.net/.auth/me " 这是我使用“ https://todolistjbb.azurewebsites.net/.auth/login/aad ”登录然后访问“ https://todolistjbb.azurewebsites.net/.auth/me ”后获得的信息 在此处输入图片说明

I feel like I have tried SO many things. 我觉得我尝试了很多事情。 I have recorded 66.68 hours working on just trying to get Authentication in my app.... please... someone tell me what I am doing wrong! 我已经记录了66.68小时的工作,它们只是试图在我的应用中获取身份验证。...请...有人告诉我我做错了! I am losing it over here :'( 我在这里丢了它:'(

According to your description, I assumed that you are using the Server-managed authentication provided by Azure App Service authentication/authorization . 根据您的描述,我假设您正在使用Azure App Service身份验证/授权提供的服务器管理的身份 验证 Since you are using the Microsoft.Azure.Mobile.Client >= 4.0.0, for your mobile client, you would leverage the following code snippet for logging via the server-flow: 由于您正在使用Microsoft.Azure.Mobile.Client> = 4.0.0,因此对于您的移动客户端,您将利用以下代码段通过服务器流进行日志记录:

var user = await client.LoginAsync(this, provider, "{url_scheme_of_your_app}");

Details you could follow Add authentication to the app . 您可以遵循的详细信息将身份验证添加到应用程序 Moreover, you need to Add your app to the Allowed External Redirect URLs . 此外,您需要将您的应用添加到允许的外部重定向URL

Based on the error message from your phone 2: 根据手机中的错误消息2:

todolistjbbservice://easyauth.callback/#authorization_code=xxxxx todolistjbbservice://easyauth.callback/#authorization_code=xxxxx

It seems that you did not configured the Authorized Redirect URI correctly. 看来您没有正确配置授权重定向URI For the Azure Active Directory provider, you could follow here for registering your Web App / API or Native application. 对于Azure Active Directory提供程序,您可以按照此处注册Web App / API或本机应用程序。 For the Google provider, you could follow here . 对于Google提供商,您可以点击此处

After correctly configured your preferred identity provider(s), you need to add your app to the Allowed External Redirect URLs: 在正确配置了首选身份提供者之后,您需要将应用添加到“允许的外部重定向URL”中:

  • Log into Azure Portal, choose your App Service 登录到Azure门户,选择您的应用程序服务
  • Click the Authentication / Authorization, enter ToDoList53172://easyauth.callback in the Allowed External Redirect URLs, and save your changes. 单击“身份验证/授权”,在“允许的外部重定向URL”中输入ToDoList53172://easyauth.callback ,然后保存更改。

The way to solve this problem is do not start with a capitalized letter for your Url Scheme. 解决此问题的方法不是以“ URL方案”的大写字母开头。 It took me over 2 weeks to figure it out. 我花了2个多星期才弄清楚。 I don't think this sis written anywhere, but I am sure it is. 我认为这篇论文没有写在任何地方,但我敢肯定。 So yeah to fix this i switched "ToDoList53172" to "todolist53172" That's it... Oy vey! 所以,要解决此问题,我将“ ToDoList53172”切换为“ todolist53172”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM