Brent Edwards
Brent Edwards

Reputation: 185

Azure Mobile Services authentication not working with Windows Phone 8.1 Universal app

I'm adding Azure Mobile Services authentication to a Universal Windows project. It's all set up and working properly on the server and in the Windows Store version of the app, however I can't get it to work with the Windows Phone 8.1 version of the app. I actually have two different apps that I've been working on with the same problem, so I created a test app based strictly on the steps outlined in this article. The sample app has one button on the UI that will attempt authenticate the user with Twitter when pressed.

The flow that I am seeing from the UI is:

  1. Press the button
  2. Screen goes black for a moment
  3. Screen displays "Resuming..." with a spinner
  4. InvalidOperationException gets caught

The exception details:

System.InvalidOperationException was caught HResult=-2146233079 Message=Authentication was cancelled by the user. Source=mscorlib

StackTrace:
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
       at Microsoft.WindowsAzure.MobileServices.MobileServiceAuthentication.<LoginAsync>d__0.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
       at MobileAuthTest.MainPage.<AuthenticateAsync>d__3.MoveNext()
  InnerException:

The code I am using is copied almost verbatim from the article, but I'll paste it here too:

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        await this.AuthenticateAsync();
    }

    // Define a method that performs the authentication process
    // using a Twitter sign-in. 
    private async Task AuthenticateAsync()
    {
        while (user == null)
        {
            string message;
            try
            {
                // Change 'MobileService' to the name of your MobileServiceClient instance.
                // Sign-in using Twitter authentication.
                user = await App.mobileService
                    .LoginAsync(MobileServiceAuthenticationProvider.Twitter);
                message = 
                    string.Format("You are now signed in - {0}", user.UserId);
            }
            catch (InvalidOperationException)
            {
                message = "You must log in. Login Required";
            }

            var dialog = new MessageDialog(message);
            dialog.Commands.Add(new UICommand("OK"));
            await dialog.ShowAsync();
        }
    }

And this override is in App.xaml.cs:

    protected override void OnActivated(IActivatedEventArgs args)
    {
        // Windows Phone 8.1 requires you to handle the respose from the WebAuthenticationBroker.
#if WINDOWS_PHONE_APP
        if (args.Kind == ActivationKind.WebAuthenticationBrokerContinuation)
        {
            // Completes the sign-in process started by LoginAsync.
            // Change 'MobileService' to the name of your MobileServiceClient instance. 
            mobileService.LoginComplete(args as WebAuthenticationBrokerContinuationEventArgs);
        }
#endif

        base.OnActivated(args);
    }

My testing is all on a real Windows Phone 8.1 device (Nokia 920 with 8.1 update).

It's also worth pointing out that the project I'm currently trying to get this to work in is also using Xamarin.Forms for Android, iOS and Windows Phone (I wanted a comparison between Xamarin.Forms for Windows Phone and Universal apps for Windows Phone). All of the apps work, EXCEPT for the two Windows Phone apps. The Xamarin.Forms Windows Phone app is a Windows Phone Silverlight 8.1 app which acts the same as described above for the Universal WP, but fails with the error, "Authentication failed with HTTP response code 0."

I've searched around and haven't found any other cases of people encountering the same issue I am. Is there something I need to do that is so simple nobody feels the need to say it in writing?

Upvotes: 0

Views: 424

Answers (1)

Brent Edwards
Brent Edwards

Reputation: 185

Well, it turns out that the issue I am seeing may just be my device. I sent the sample app to another dev for testing and he had no issues. I also was able to successfully log in with both the sample app and the real app on the simulator. That isn't exactly a thorough test sample, but it's enough for me to stop banging my head on this desk.

Upvotes: 0

Related Questions