The following forum(s) have migrated to Microsoft Q&A (Preview): Developing Universal Windows apps!
Visit Microsoft Q&A (Preview) to post new questions.

Learn More

 locked
[UWP]Connecting Existing Windows 10 UWP App to Azure Mobile Service for Google Authentication RRS feed

  • Question

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

    I also added the google  google app clientid and secret to my azure mobile app service with .net backend in 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

    message after clicking signin button

    pretty much stuck here 

    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


    Friday, May 27, 2016 9:24 AM

Answers

  • 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

    Install Microsoft.Azure.Mobile.Client

    That is instead of the WindowsAzureMobileService 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 fromhttp://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

    • Marked as answer by JOEL JOSEPH Friday, May 27, 2016 9:43 PM
    Friday, May 27, 2016 9:43 PM

All replies

  • 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

    Install Microsoft.Azure.Mobile.Client

    That is instead of the WindowsAzureMobileService 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 fromhttp://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

    • Marked as answer by JOEL JOSEPH Friday, May 27, 2016 9:43 PM
    Friday, May 27, 2016 9:43 PM
  • Hi Joel Joseph,

    Glad to know you found the solution.


    Thanks MSDN Community Support Please remember to Mark as Answer the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, May 30, 2016 3:57 AM