【发布时间】:2015-04-24 13:12:03
【问题描述】:
我有一个 ASP.NET MVC 4 应用程序。 昨天我的用户开始抱怨他们无法使用他们的 Google 帐户登录。经过大量谷歌搜索后,我发现了这个:DotNetOpenAuth.GoogleOAuth2。我按照说明进行操作。
我在 Google 控制台中为 Web 应用程序创建了客户端 ID。
在 AuthConfig.RegisterAuth() 我有:
var client = new DotNetOpenAuth.GoogleOAuth2.GoogleOAuth2Client(googleClientID, googleClientSecret);
var extraData = new Dictionary<string, object>();
OAuthWebSecurity.RegisterClient(client, "Google", extraData);
在 AccountController 中,我有这样的东西:
public ActionResult ExternalLoginCallback(string returnUrl)
{
DotNetOpenAuth.GoogleOAuth2.GoogleOAuth2Client.RewriteRequest();
AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl }));
if (!result.IsSuccessful)
{
return RedirectToAction("ExternalLoginFailure");
}
if (OAuthWebSecurity.Login(result.Provider, result.ProviderUserId, createPersistentCookie: false))
{
// here I have some logic where is user sent when login was successfull
return RedirectToLocal(returnUrl);
}
if (User.Identity.IsAuthenticated)
{
// If the current user is logged in add the new account
OAuthWebSecurity.CreateOrUpdateAccount(result.Provider, result.ProviderUserId, User.Identity.Name);
return RedirectToLocal(returnUrl);
}
else
{
// User is new, ask for their desired membership name
string loginData = OAuthWebSecurity.SerializeProviderUserId(result.Provider, result.ProviderUserId);
// some logic
return View("ExternalLoginConfirmation", new RegisterExternalLoginModel { UserName = username, ExternalLoginData = loginData, EncryptedEmail = encryptedEmail });
}
}
我有两个问题:
在更改之前,
result.UserName包含用户电子邮件。现在它包含名称。但我需要电子邮件。除此之外,注册工作正常。我最大的问题 - 现有用户无法使用他们的 google 帐户登录。代码为他们转到
"// User is new, ask for their desired membership name"。我现在得到的 ProviderUserId 对于同一个邮箱地址是不同的。
非常感谢您的建议。
【问题讨论】:
-
请问,有人知道吗?我被卡住了,我的用户无法登录。我相信很多人都不得不面对这个问题。非常感谢您的帮助。
标签: asp.net-mvc asp.net-mvc-4 oauth-2.0 google-oauth