当我开始我的项目时,Google 允许使用 OpenID,无需真正注册。 现在这种情况已经消失了,我已将项目移植到新的方法中..但是 问题是用户将获得新的安全 token ,因此将该用户视为新用户。
你们中有人解决了这个问题吗?这样我就不需要要求所有用户创建新帐户,然后将旧帐户与新帐户链接起来?
最佳答案
我决定将 Google 电子邮件作为我与现有用户的链接。 如果您在 ASP.NET/WEBAPI 中使用 Identity 的标准实现,则更改:AccountControler.cs -ExternalLoginCallback
如果 Google 用户不存在,则查找电子邮件并向现有用户添加登录信息,如下所示:
// Sign in the user with this external login provider if the user already has a login
var user = await UserManager.FindAsync(loginInfo.Login);
插入:
//Google Migration. If not existing then check if the Google email exists and if it does the change the LoginProvider key and try to login again
if (user == null && loginInfo.Login.LoginProvider == "Google" && email != "")
{
xxxEntities db = new xxxEntities();
var existingUser = db.user_profiles.Where(e => e.eMail == (string)email).Where(p => p.created_from == "Google");
if (existingUser.Count() > 0)
{
var existingID = existingUser.Single().userFK;
var result = await UserManager.AddLoginAsync(existingID, loginInfo.Login);
if (result.Succeeded)
{
//Now retry the check if the user with this external login provider if the user already has a login
user = await UserManager.FindAsync(loginInfo.Login);
}
}
}
到这里:
//Now for the normal Check
if (user != null)
{
请注意,查找是我拥有的一些内部对象,可以通过将电子邮件查找添加到 UserManager 在 OOTB 实现中完成,或者像我一样在您自己的用户管理对象中查找它
关于asp.net-web-api - WebApi 2 从 Google 身份验证 OpenID 更改 -> oAuth2 - 更改安全句柄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28139204/