gpt4 book ai didi

c# - 使用 Azure 移动服务对现有 Windows 10 UWP 应用程序进行 Google 身份验证

转载 作者:行者123 更新时间:2023-12-03 05:07:32 26 4
gpt4 key购买 nike

我尝试在 Windows 10 通用应用程序中实现 google Login 。

我还在 azure 门户中通过 .net 后端将 google 应用客户端 ID 和 key 添加到了我的 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");

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

Google Login Window

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

Error Shown on Login

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

如何解决这个错误

最佳答案

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

卸载

首先您需要卸载 Windows Azure Mobile Service SDK

uninstall mobile service sdk

安装 Microsoft.Azure.Mobile.Client

这不是安装WindowsAzureMobileService SDK而是安装Azure Mobile App SDK

Install Azure Mobile App sdk

更改授权重定向 URI

此外,Google 应用程序 console.developers.google.com 中的回复 URL 也必须更改。

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

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

确保您正在使用HTTPS

现在 Google 身份验证可以运行

关于c# - 使用 Azure 移动服务对现有 Windows 10 UWP 应用程序进行 Google 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37485643/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com