0
votes

Azure Active Directory

Google+ Auth

Xamarin Forms, PCL (NuGet 2.4.0.282)

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): enter image description here

Phone 1 (Google Auth it greys out and just keeps "loading") enter image description here

Phone 2 (AAD and Google Auth): enter image description here

Emulator (AAD and Google Auth): enter image description here

I have done everything I found here on Stack OverFlow, that makes sense and seems to be applicable to current versions of NuGets. This person seems to be having a similar issue to me but with Google Log in Azure not redirecting after loginenter link description here

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

And I get the same results. I have tried both AAD and 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. 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:

<?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. It is all in the Xamarin sample project posted above. 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

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. The code above is for Google.

Here is my Azure Auth setup enter image description here

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" enter image description here

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! I am losing it over here :'(

2

2 Answers

1
votes

The way to solve this problem is do not start with a capitalized letter for your Url Scheme. It took me over 2 weeks to figure it out. 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!

0
votes

According to your description, I assumed that you are using the Server-managed authentication provided by Azure App Service authentication/authorization. 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:

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.

Based on the error message from your phone 2:

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

It seems that you did not configured the Authorized Redirect URI correctly. For the Azure Active Directory provider, you could follow here for registering your Web App / API or Native application. For the Google provider, you could follow here.

After correctly configured your preferred identity provider(s), you need to add your app to the Allowed External Redirect URLs:

  • Log into Azure Portal, choose your App Service
  • Click the Authentication / Authorization, enter ToDoList53172://easyauth.callback in the Allowed External Redirect URLs, and save your changes.