Joel Joseph
Joel Joseph

Reputation: 6169

Google Authentication For Existing Windows 10 UWP App Using Azure Mobile Service

I tried to implement google Login in a Windows 10 Universal App .

I also added the google app client id and secret to my azure mobile app service with .net backend in the azure portal and enabled google login. I installed AzureMobileService SDK added the following code in LoginPage.xaml.cs

    // Define a member variable for storing the signed-in user. 
        private MobileServiceUser user;

 // Define a method that performs the authentication process
        // using a Google sign-in. 
        private async System.Threading.Tasks.Task<bool> AuthenticateAsync()
        {
            string message;
            bool success = false;

            // This sample uses the Google provider.
            var provider = MobileServiceAuthenticationProvider.Google;

            // Use the PasswordVault to securely store and access credentials.
            PasswordVault vault = new PasswordVault();
            PasswordCredential credential = null;

            try
            {
                // Try to get an existing credential from the vault.
                credential = vault.FindAllByResource(provider.ToString()).FirstOrDefault();
            }
            catch (Exception)
            {
                // When there is no matching resource an error occurs, which we ignore.
            }

            if (credential != null)
            {
                // Create a user from the stored credentials.
                user = new MobileServiceUser(credential.UserName);
                credential.RetrievePassword();
                user.MobileServiceAuthenticationToken = credential.Password;

                // Set the user from the stored credentials.
                App.MobileService.CurrentUser = user;

                // Consider adding a check to determine if the token is 
                // expired, as shown in this post: http://aka.ms/jww5vp.

                success = true;
                message = string.Format("Cached credentials for user - {0}", user.UserId);
            }
            else
            {
                try
                {
                    // Login with the identity provider.
                    user = await App.MobileService
                        .LoginAsync(provider);

                    // Create and store the user credentials.
                    credential = new PasswordCredential(provider.ToString(),
                        user.UserId, user.MobileServiceAuthenticationToken);
                    vault.Add(credential);

                    success = true;
                    message = string.Format("You are now logged in - {0}", user.UserId);
                }
                catch (MobileServiceInvalidOperationException)
                {
                    message = "You must log in. Login Required";
                }
            }

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

            return success;
        }


private async void ButtonLogin_Click(object sender, RoutedEventArgs e)
{
    // Login the user and then load data from the mobile app.
    if (await AuthenticateAsync())
    {
         var dialog = new MessageDialog("Loged in");
                await dialog.ShowAsync();

    }
}

And in App.xaml.cs added the following code

// Connect to azure  
        public static MobileServiceClient MobileService = new MobileServiceClient("https://xxxxxxxxxxxxxxx.azurewebsites.net");

On clicking the login button the google signing windows pop ups and after entering the username and password i tried to sign in the following message is shown

Google Login Window

The error message shown "We can't connect to the service you need right now .Check your network connection and try this again later"

Error Shown on Login

In the console.developers.google.com i have enabled my google app api and added Authorized redirect URIs as :http://xxxxxxxxxxxx.azurewebsites.net/.auth/login/google/callback

How can i solve this error

Upvotes: 0

Views: 1143

Answers (2)

Joel Joseph
Joel Joseph

Reputation: 6169

I found that one of the error was caused by the Windows Azure Mobile Service SDK Installed with the Nuget. It seems that the old client ( Windows Azure Mobile Service SDK) is calling the wrong URL

Uninstall

First you need to Uninstall the Windows Azure Mobile Service SDK

uninstall mobile service sdk

Install Microsoft.Azure.Mobile.Client

That is instead of the WindowsAzureMobileService SDK install Azure Mobile App SDK

Install Azure Mobile App sdk

Change Authorized redirect URI

Also the reply URL in the Google application at console.developers.google.com has to be changed.

That is the Authorized redirect URI set at console.developers.google.com has to be changed from http://xxxxxxxxxxxx.azurewebsites.net/.auth/login/google/callback

to

https://xxxxxxxxxxxx.azurewebsites.net/.auth/login/google/callback

Make sure you that are using using HTTPS

Now the Google Authentication Works

Upvotes: 2

Chris Gillum
Chris Gillum

Reputation: 15052

The best way to debug these kinds of authentication issues is to enable Application Logging in your app. You can do this from the Azure management portal. More details here:

https://azure.microsoft.com/en-us/documentation/articles/web-sites-enable-diagnostic-log/

Once enabled, try your login again. You should be able to see what the problem is in the logs using either Kudu to access the log files directly or by using the Log Streaming tool in the portal.

Upvotes: 1

Related Questions