WebApi 2从Google身份验证OpenID更改 - > oAuth2 - 更改安全句柄

时间:2015-01-25 17:22:38

标签: asp.net-web-api openid google-oauth

当我开始我的项目时,Google允许使用OpenID,而不需要真正的注册。 现在这已经消失了,我已将项目移植到新的方法中......但是 问题是用户将获得一个新的安全令牌,因此将该用户视为新用户。

你们有没有解决这个问题,所以我不需要让所有用户创建新帐户,然后让我将旧帐户与新帐户相关联?

1 个答案:

答案 0 :(得分:2)

我认为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实现中完成,或者就像我一样,并在您自己的用户管理对象中查找