asp.net-web-api - WebApi 2 从 Google 身份验证 OpenID 更改 -> oAuth2 - 更改安全句柄

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

当我开始我的项目时,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/

相关文章:

c# - 测试 Microsoft ASP.NET Web API ---- 内存中

python - 在 Tornado python中设置openId

database-design - 在数据库中存储 OpenID 信息

google-app-engine - https ://appengine. google.com/_ah/logout 还在工作吗?

c# - IdentityServer4 - 缺少来自 Google 的声明

.Net 核心 CORS 401 发布/放置

c# - 使用 c# NAudio 将 WAV 转换为 M4A

时间:2019-03-08 标签:c#webapihttpgetattribute

ionic-framework - 身份服务器 + ionic + OpenID 混合流

django - create_user() 缺少 1 个必需的位置参数 : 'password'