【问题标题】:Google Authentication For Existing Windows 10 UWP App Using Azure Mobile Service使用 Azure 移动服务对现有 Windows 10 UWP 应用进行 Google 身份验证
【发布时间】:2016-05-27 14:10:38
【问题描述】:

我尝试在 Windows 10 通用应用中实现谷歌登录。

我还在 azure 门户中使用 .net 后端将 google 应用程序客户端 ID 和密码添加到我的 azure 移动应用程序服务,并启用了 google 登录。 我安装了 AzureMobileService SDK 在 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();

    }
}

并在 App.xaml.cs 中添加如下代码

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

点击登录按钮后,谷歌签名窗口弹出,输入用户名和密码后,我尝试登录,显示以下消息

错误消息显示“我们现在无法连接到您需要的服务。请检查您的网络连接,稍后再试”

在 console.developers.google.com 中,我启用了我的 google app api,并将授权重定向 URI 添加为:http://xxxxxxxxxxxx.azurewebsites.net/.auth/login/google/callback

我该如何解决这个错误

【问题讨论】:

    标签: c# azure uwp azure-mobile-services google-signin


    【解决方案1】:

    我发现其中一个错误是由随 Nuget 安装的 Windows Azure Mobile Service SDK 引起的。旧客户端(Windows Azure Mobile Service SDK)似乎调用了错误的URL

    卸载

    首先您需要卸载 Windows Azure 移动服务 SDK

    安装 Microsoft.Azure.Mobile.Client

    即代替 WindowsAzureMobileService SDK 安装 Azure 移动应用 SDK

    更改授权重定向 URI

    还必须更改位于 console.developers.google.com 的 Google 应用程序中的回复 URL

    即在 console.developers.google.com 设置的授权重定向 URI 必须从 http://xxxxxxxxxxxx.azurewebsites.net/.auth/login/google/callback 更改

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

    确保您使用的是 HTTPS

    现在谷歌身份验证工作

    【讨论】:

      【解决方案2】:

      调试此类身份验证问题的最佳方法是在您的应用中启用应用程序日志记录。您可以从 Azure 管理门户执行此操作。更多细节在这里:

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

      启用后,再次尝试登录。您应该能够使用 Kudu 直接访问日志文件或使用门户中的 Log Streaming 工具查看日志中的问题。

      【讨论】:

      • 谢谢我已经解决了这个问题。我已经在下面发布了解决方案
      猜你喜欢
      • 2016-06-28
      • 2016-07-13
      • 2015-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-24
      • 1970-01-01
      相关资源
      最近更新 更多