Exemplo n.º 1
0
        public IActionResult AuthCallback(LoginBase.AuthorizeResult authorizeResult)
        {
            var vm = new ActionResultVM();

            try
            {
                if (string.IsNullOrWhiteSpace(authorizeResult.code))
                {
                    vm.Set(ARTag.unauthorized);
                }
                else
                {
                    //唯一标示
                    string openId = string.Empty;
                    //注册信息
                    var mo = new Domain.UserInfo()
                    {
                        LoginLimit     = 0,
                        UserSex        = 0,
                        UserCreateTime = DateTime.Now
                    };
                    //头像
                    string avatar = string.Empty;
                    //头像(高清)
                    string avatarhd = string.Empty;

                    Enum.TryParse(RouteData.Values["id"]?.ToString(), true, out ValidateloginType vtype);

                    switch (vtype)
                    {
                    case ValidateloginType.qq:
                    {
                        //获取 access_token
                        var tokenEntity = QQ.AccessToken(new QQ_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        //获取 OpendId
                        var openidEntity = QQ.OpenId(new QQ_OpenId_RequestEntity()
                            {
                                access_token = tokenEntity.access_token
                            });

                        //获取 UserInfo
                        var userEntity = QQ.OpenId_Get_User_Info(new QQ_OpenAPI_RequestEntity()
                            {
                                access_token = tokenEntity.access_token,
                                openid       = openidEntity.openid
                            });

                        //身份唯一标识
                        openId     = openidEntity.openid;
                        mo.OpenId1 = openId;

                        mo.Nickname = userEntity.nickname;
                        mo.UserSex  = userEntity.gender == "男" ? 1 : 2;
                        mo.UserSay  = "";
                        mo.UserUrl  = "";

                        avatar   = userEntity.figureurl_qq_1;
                        avatarhd = userEntity.figureurl_qq_2;
                    }
                    break;

                    case ValidateloginType.weibo:
                    {
                        //获取 access_token
                        var tokenEntity = Weibo.AccessToken(new Weibo_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        //获取 access_token 的授权信息
                        var tokenInfoEntity = Weibo.GetTokenInfo(new Weibo_GetTokenInfo_RequestEntity()
                            {
                                access_token = tokenEntity.access_token
                            });

                        //获取 users/show
                        var userEntity = Weibo.UserShow(new Weibo_UserShow_RequestEntity()
                            {
                                access_token = tokenEntity.access_token,
                                uid          = Convert.ToInt64(tokenInfoEntity.uid)
                            });

                        openId     = tokenEntity.access_token;
                        mo.OpenId2 = openId;

                        mo.Nickname = userEntity.screen_name;
                        mo.UserSex  = userEntity.gender == "m" ? 1 : userEntity.gender == "f" ? 2 : 0;
                        mo.UserSay  = userEntity.description;
                        mo.UserUrl  = userEntity.domain;

                        avatar   = userEntity.profile_image_url;
                        avatarhd = userEntity.avatar_large;
                    }
                    break;

                    case ValidateloginType.github:
                    {
                        //获取 access_token
                        var tokenEntity = GitHub.AccessToken(new GitHub_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        //获取 user
                        var userEntity = GitHub.User(new GitHub_User_RequestEntity()
                            {
                                access_token = tokenEntity.access_token
                            });

                        openId     = userEntity.id.ToString();
                        mo.OpenId3 = openId;

                        mo.Nickname = userEntity.name;
                        mo.UserSay  = userEntity.bio;
                        mo.UserUrl  = userEntity.blog;
                        mo.UserMail = userEntity.email;

                        avatar   = userEntity.avatar_url;
                        avatarhd = userEntity.avatar_url;
                    }
                    break;

                    case ValidateloginType.taobao:
                    {
                        //获取 access_token
                        var tokenEntity = TaoBao.AccessToken(new TaoBao_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        openId     = tokenEntity.open_uid;
                        mo.OpenId4 = openId;

                        mo.Nickname = "淘宝用户";
                    }
                    break;

                    case ValidateloginType.microsoft:
                    {
                        //获取 access_token
                        var tokenEntity = MicroSoft.AccessToken(new MicroSoft_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        //获取 user
                        var userEntity = MicroSoft.User(new MicroSoft_User_RequestEntity()
                            {
                                access_token = tokenEntity.access_token
                            });

                        openId     = userEntity.id.ToString();
                        mo.OpenId5 = openId;

                        mo.Nickname = userEntity.last_name + userEntity.first_name;
                        mo.UserMail = userEntity.emails["account"].ToStringOrEmpty();
                    }
                    break;

                    case ValidateloginType.dingtalk:
                    {
                        //获取 user
                        var userEntity = DingTalk.User(new DingTalk_User_RequestEntity(), authorizeResult.code);

                        openId     = userEntity.openid;
                        mo.OpenId6 = openId;

                        mo.Nickname = userEntity.nick;
                    }
                    break;
                    }

                    mo.UserCreateTime = DateTime.Now;
                    mo.UserName       = openId;
                    mo.UserPwd        = Core.CalcTo.MD5(openId);
                    if (!string.IsNullOrWhiteSpace(avatar))
                    {
                        mo.UserPhoto = Core.UniqueTo.LongId().ToString() + ".jpg";
                    }

                    if (string.IsNullOrWhiteSpace(openId))
                    {
                        vm.Set(ARTag.unauthorized);
                        vm.msg = "身份验证失败";
                    }
                    else
                    {
                        //判断是绑定操作
                        bool isbind = User.Identity.IsAuthenticated && authorizeResult.state.StartsWith("bind");
                        if (isbind)
                        {
                            int uid = new Func.UserAuthAid(HttpContext).Get().UserId;

                            using (var db = new ContextBase())
                            {
                                //检测是否绑定其它账号
                                var queryIsBind = db.UserInfo.Where(x => x.UserId != uid);
                                switch (vtype)
                                {
                                case ValidateloginType.qq:
                                    queryIsBind = queryIsBind.Where(x => x.OpenId1 == openId);
                                    break;

                                case ValidateloginType.weibo:
                                    queryIsBind = queryIsBind.Where(x => x.OpenId2 == openId);
                                    break;

                                case ValidateloginType.github:
                                    queryIsBind = queryIsBind.Where(x => x.OpenId3 == openId);
                                    break;

                                case ValidateloginType.taobao:
                                    queryIsBind = queryIsBind.Where(x => x.OpenId4 == openId);
                                    break;

                                case ValidateloginType.microsoft:
                                    queryIsBind = queryIsBind.Where(x => x.OpenId5 == openId);
                                    break;

                                case ValidateloginType.dingtalk:
                                    queryIsBind = queryIsBind.Where(x => x.OpenId6 == openId);
                                    break;
                                }
                                if (queryIsBind.Count() > 0)
                                {
                                    return(Content("已绑定其它账号,不能重复绑定"));
                                }

                                var userInfo = db.UserInfo.Find(uid);

                                switch (vtype)
                                {
                                case ValidateloginType.qq:
                                    userInfo.OpenId1 = openId;
                                    break;

                                case ValidateloginType.weibo:
                                    userInfo.OpenId2 = openId;
                                    break;

                                case ValidateloginType.github:
                                    userInfo.OpenId3 = openId;
                                    break;

                                case ValidateloginType.taobao:
                                    userInfo.OpenId4 = openId;
                                    break;

                                case ValidateloginType.microsoft:
                                    userInfo.OpenId5 = openId;
                                    break;

                                case ValidateloginType.dingtalk:
                                    userInfo.OpenId6 = openId;
                                    break;
                                }
                                db.UserInfo.Update(userInfo);
                                db.SaveChanges();
                            }

                            return(Redirect("/user/setting"));
                        }
                        else
                        {
                            using var db = new ContextBase();
                            Domain.UserInfo vmo = null;
                            switch (vtype)
                            {
                            case ValidateloginType.qq:
                                vmo = db.UserInfo.FirstOrDefault(x => x.OpenId1 == openId);
                                break;

                            case ValidateloginType.weibo:
                                vmo = db.UserInfo.FirstOrDefault(x => x.OpenId2 == openId);
                                break;

                            case ValidateloginType.github:
                                vmo = db.UserInfo.FirstOrDefault(x => x.OpenId3 == openId);
                                break;

                            case ValidateloginType.taobao:
                                vmo = db.UserInfo.FirstOrDefault(x => x.OpenId4 == openId);
                                break;

                            case ValidateloginType.microsoft:
                                vmo = db.UserInfo.FirstOrDefault(x => x.OpenId5 == openId);
                                break;

                            case ValidateloginType.dingtalk:
                                vmo = db.UserInfo.FirstOrDefault(x => x.OpenId6 == openId);
                                break;
                            }
                            //未注册
                            if (vmo == null)
                            {
                                var ruvm = RegisterUser(mo);
                                if (ruvm.code == 200)
                                {
                                    vm = ValidateLogin(vtype, mo);
                                    //拉取头像
                                    if (vm.code == 200 && (!string.IsNullOrWhiteSpace(avatar) || !string.IsNullOrWhiteSpace(avatarhd)))
                                    {
                                        try
                                        {
                                            using var wc = new System.Net.WebClient();
                                            var rootdir  = GlobalTo.WebRootPath + "/" + (GlobalTo.GetValue("StaticResource:RootDir").TrimStart('/').TrimEnd('/') + "/");
                                            var path     = GlobalTo.GetValue("StaticResource:AvatarPath").TrimEnd('/').TrimStart('/') + '/';
                                            var fullpath = rootdir + path;

                                            if (!System.IO.Directory.Exists(fullpath))
                                            {
                                                System.IO.Directory.CreateDirectory(fullpath);
                                            }
                                            if (!string.IsNullOrWhiteSpace(avatar))
                                            {
                                                wc.DownloadFile(avatar, fullpath + mo.UserPhoto);
                                            }
                                            if (!string.IsNullOrWhiteSpace(avatarhd))
                                            {
                                                wc.DownloadFile(avatarhd, fullpath + mo.UserPhoto.Replace(".jpg", "_lg.jpg"));
                                            }
                                        }
                                        catch (Exception)
                                        {
                                        }
                                    }
                                }
                                else
                                {
                                    vm.msg = ruvm.msg;
                                }
                            }
                            else
                            {
                                vm = ValidateLogin(vtype, vmo);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                vm.Set(ex);
            }

            //成功
            if (vm.code == 200)
            {
                return(Redirect("/"));
            }
            else
            {
                string msg = "【登录失败】( " + vm.msg + " )".ToEncode();
                return(Redirect("/home/error?msg=" + msg));
            }
        }
Exemplo n.º 2
0
        public ActionResult AuthCallback(string code)
        {
            var    result = new AccountValidationVM();
            string vtype  = RouteData.Values["id"]?.ToString().ToLower();

            try
            {
                //唯一标示
                string openId = string.Empty;
                try
                {
                    switch (vtype)
                    {
                    case "qq":
                    {
                        //获取 access_token
                        var accessToken_ResultEntity = QQ.AccessToken(new QQ_AccessToken_RequestEntity()
                            {
                                code = code
                            });

                        //获取 OpendId
                        var openId_ResultEntity = QQ.OpenId(new QQ_OpenId_RequestEntity()
                            {
                                access_token = accessToken_ResultEntity.access_token
                            });

                        //获取 UserInfo
                        var openId_Get_User_Info_ResultEntity = QQ.OpenId_Get_User_Info(new QQ_OpenAPI_RequestEntity()
                            {
                                access_token = accessToken_ResultEntity.access_token,
                                openid       = openId_ResultEntity.openid
                            });

                        //身份唯一标识
                        openId = openId_ResultEntity.openid;
                    }
                    break;

                    case "weibo":
                    {
                        //获取 access_token
                        var accessToken_ResultEntity = Weibo.AccessToken(new Weibo_AccessToken_RequestEntity()
                            {
                                code = code
                            });

                        //获取 access_token 的授权信息
                        var tokenInfo_ResultEntity = Weibo.GetTokenInfo(new Weibo_GetTokenInfo_RequestEntity()
                            {
                                access_token = accessToken_ResultEntity.access_token
                            });

                        //获取 users/show
                        var userShow_ResultEntity = Weibo.UserShow(new Weibo_UserShow_RequestEntity()
                            {
                                access_token = accessToken_ResultEntity.access_token,
                                uid          = Convert.ToInt64(tokenInfo_ResultEntity.uid)
                            });

                        openId = accessToken_ResultEntity.access_token;
                    }
                    break;

                    case "github":
                    {
                        //获取 access_token
                        var accessToken_ResultEntity = GitHub.AccessToken(new GitHub_AccessToken_RequestEntity()
                            {
                                code = code
                            });

                        //获取 user
                        var user_ResultEntity = GitHub.User(new GitHub_User_RequestEntity()
                            {
                                access_token = accessToken_ResultEntity.access_token
                            });

                        openId = user_ResultEntity.id.ToString();
                    }
                    break;

                    case "taobao":
                    {
                        //获取 access_token
                        var accessToken_ResultEntity = Taobao.AccessToken(new Taobao_AccessToken_RequestEntity()
                            {
                                code = code
                            });

                        openId = accessToken_ResultEntity.open_uid;
                    }
                    break;

                    case "microsoft":
                    {
                        //获取 access_token
                        var accessToken_ResultEntity = MicroSoft.AccessToken(new MicroSoft_AccessToken_RequestEntity()
                            {
                                code = code
                            });

                        //获取 user
                        var user_ResultEntity = MicroSoft.User(new MicroSoft_User_RequestEntity()
                            {
                                access_token = accessToken_ResultEntity.access_token
                            });

                        openId = user_ResultEntity.id.ToString();
                    }
                    break;
                    }
                }
                catch (Exception ex)
                {
                    result.message = ex.Message;
                }

                if (string.IsNullOrWhiteSpace(openId))
                {
                    result.message = "身份验证失败";
                }
                else
                {
                    //判断是绑定操作
                    bool isbind = false;
                    if (User.Identity.IsAuthenticated)
                    {
                        var aboa = Request.Cookies["AccountBindOAuth"]?.Value;
                        if (!string.IsNullOrWhiteSpace(aboa) && (DateTime.Now - DateTime.Parse(aboa)).TotalSeconds < 120)
                        {
                            string uid = Func.Common.GetLoginUserInfo().UserId;

                            using (var db = new ContextBase())
                            {
                                var sysauth = db.SysAuthorize.Where(x => x.SuId == uid).FirstOrDefault();
                                var isadd   = sysauth == null;
                                //新增
                                if (isadd)
                                {
                                    sysauth = new SysAuthorize()
                                    {
                                        SaId = Guid.NewGuid().ToString(),
                                        SuId = uid
                                    };
                                }

                                switch (vtype)
                                {
                                case "qq":
                                    sysauth.OpenId1 = openId;
                                    break;

                                case "weibo":
                                    sysauth.OpenId2 = openId;
                                    break;

                                case "github":
                                    sysauth.OpenId3 = openId;
                                    break;

                                case "taobao":
                                    sysauth.OpenId4 = openId;
                                    break;

                                case "microsoft":
                                    sysauth.OpenId5 = openId;
                                    break;
                                }
                                if (isadd)
                                {
                                    db.SysAuthorize.Add(sysauth);
                                }
                                else
                                {
                                    db.SysAuthorize.Update(sysauth, db);
                                }
                                db.SaveChanges();
                            }

                            Response.Cookies.Remove("AccountBindOAuth");
                            isbind = true;

                            result.code    = 301;
                            result.message = "绑定成功";
                            result.url     = "/";
                        }
                    }

                    //非绑定操作
                    if (!isbind)
                    {
                        using (var db = new ContextBase())
                        {
                            SysUser vmo = null;
                            switch (vtype)
                            {
                            case "qq":
                                vmo = (from a in db.SysAuthorize
                                       join b in db.SysUser on a.SuId equals b.SuId
                                       where a.OpenId1 == openId
                                       select b).FirstOrDefault();
                                break;

                            case "weibo":
                                vmo = (from a in db.SysAuthorize
                                       join b in db.SysUser on a.SuId equals b.SuId
                                       where a.OpenId2 == openId
                                       select b).FirstOrDefault();
                                break;

                            case "github":
                                vmo = (from a in db.SysAuthorize
                                       join b in db.SysUser on a.SuId equals b.SuId
                                       where a.OpenId3 == openId
                                       select b).FirstOrDefault();
                                break;

                            case "taobao":
                                vmo = (from a in db.SysAuthorize
                                       join b in db.SysUser on a.SuId equals b.SuId
                                       where a.OpenId4 == openId
                                       select b).FirstOrDefault();
                                break;

                            case "microsoft":
                                vmo = (from a in db.SysAuthorize
                                       join b in db.SysUser on a.SuId equals b.SuId
                                       where a.OpenId5 == openId
                                       select b).FirstOrDefault();
                                break;
                            }

                            //没关联
                            if (vmo == null)
                            {
                                result.code    = 302;
                                result.message = "未关联账号,先账号密码登录再关联才能使用";
                            }
                            else
                            {
                                result = LoginValidation(vmo, "_pass_", 1);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                result.message = ex.Message;
            }

            //成功
            if (result.code == 100)
            {
                return(Redirect(result.url));
            }
            else
            {
                return(View(result));
            }
        }
Exemplo n.º 3
0
            /// <summary>
            /// 回调方法
            /// </summary>
            /// <param name="authorizeResult">接收授权码、防伪参数</param>
            public void AuthCallback(LoginBase.AuthorizeResult authorizeResult)
            {
                if (!string.IsNullOrWhiteSpace(authorizeResult.code) || !string.IsNullOrWhiteSpace(authorizeResult.auth_code))
                {
                    //唯一标识
                    string OpenId = string.Empty;

                    switch (loginType)
                    {
                    case LoginBase.LoginType.QQ:
                    {
                        //获取 access_token
                        var tokenEntity = QQ.AccessToken(new QQ_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        //获取 OpendId
                        var openidEntity = QQ.OpenId(tokenEntity.access_token);

                        //获取 UserInfo
                        _ = QQ.OpenId_Get_User_Info(new QQ_OpenAPI_RequestEntity()
                            {
                                access_token = tokenEntity.access_token,
                                openid       = openidEntity.openid
                            });

                        //身份唯一标识
                        OpenId = openidEntity.openid;
                    }
                    break;

                    case LoginBase.LoginType.WeiBo:
                    {
                        //获取 access_token
                        var tokenEntity = Weibo.AccessToken(new Weibo_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        //获取 access_token 的授权信息
                        var tokenInfoEntity = Weibo.GetTokenInfo(tokenEntity.access_token);

                        //获取 users/show
                        _ = Weibo.UserShow(new Weibo_UserShow_RequestEntity()
                            {
                                access_token = tokenEntity.access_token,
                                uid          = Convert.ToInt64(tokenInfoEntity.uid)
                            });

                        OpenId = tokenEntity.access_token;
                    }
                    break;

                    case LoginBase.LoginType.WeChat:
                    {
                        //获取 access_token
                        var tokenEntity = Login.WeChat.AccessToken(new WeChat_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        //获取 user
                        _ = Login.WeChat.Get_User_Info(new WeChat_OpenAPI_RequestEntity()
                            {
                                access_token = tokenEntity.access_token,
                                openid       = tokenEntity.openid
                            });

                        //身份唯一标识
                        OpenId = tokenEntity.openid;
                    }
                    break;

                    case LoginBase.LoginType.GitHub:
                    {
                        //获取 access_token
                        var tokenEntity = GitHub.AccessToken(new GitHub_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        //获取 user
                        var userEntity = GitHub.User(tokenEntity.access_token);

                        OpenId = userEntity.id.ToString();
                    }
                    break;

                    case LoginBase.LoginType.TaoBao:
                    {
                        //获取 access_token
                        var tokenEntity = TaoBao.AccessToken(new TaoBao_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        OpenId = tokenEntity.open_uid;
                    }
                    break;

                    case LoginBase.LoginType.MicroSoft:
                    {
                        //获取 access_token
                        var tokenEntity = MicroSoft.AccessToken(new MicroSoft_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        //获取 user
                        var userEntity = MicroSoft.User(tokenEntity.access_token);

                        OpenId = userEntity.id.ToString();
                    }
                    break;

                    case LoginBase.LoginType.DingTalk:
                    {
                        //获取 user
                        var userEntity = DingTalk.User(new DingTalk_User_RequestEntity(), authorizeResult.code);

                        OpenId = userEntity?.openid;
                    }
                    break;

                    case LoginBase.LoginType.Gitee:
                    {
                        //获取 access_token
                        var tokenEntity = Gitee.AccessToken(new Gitee_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        //获取 user
                        var userEntity = Gitee.User(tokenEntity.access_token);

                        OpenId = userEntity.id.ToString();
                    }
                    break;

                    case LoginBase.LoginType.Google:
                    {
                        //获取 access_token
                        var tokenEntity = Google.AccessToken(new Google_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        //获取 user
                        var userEntity = Google.User(tokenEntity.access_token);

                        OpenId = userEntity.sub;
                    }
                    break;

                    case LoginBase.LoginType.AliPay:
                    {
                        //获取 access_token
                        var tokenEntity = AliPay.AccessToken(new AliPay_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.auth_code
                            });

                        //实际上这一步已经获取到 OpenId,登录验证可以了,获取个人信息还需调用下面的接口
                        //tokenEntity.user_id

                        //获取 user
                        var userEntity = AliPay.User(new AliPay_User_RequestEntity()
                            {
                                auth_token = tokenEntity.access_token
                            });

                        OpenId = userEntity.user_id;
                    }
                    break;

                    case LoginBase.LoginType.StackOverflow:
                    {
                        //获取 access_token
                        var tokenEntity = StackOverflow.AccessToken(new StackOverflow_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        //获取 user
                        var userEntity = StackOverflow.User(new StackOverflow_User_RequestEntity()
                            {
                                access_token = tokenEntity.access_token
                            });

                        OpenId = userEntity.user_id;
                    }
                    break;
                    }

                    //拿到登录标识
                    if (string.IsNullOrWhiteSpace(OpenId))
                    {
                        //TO DO
                    }
                }
            }
Exemplo n.º 4
0
        /// <summary>
        /// 回调方法
        /// </summary>
        /// <param name="code">请求链接得到的code</param>
        /// <param name="loginType">登录类型</param>
        public void AuthCallback(string code, LoginBase.LoginType loginType)
        {
            if (string.IsNullOrWhiteSpace(code))
            {
                //打开链接没登录授权
            }
            else
            {
                //唯一标示
                string openId = string.Empty;

                switch (loginType)
                {
                case LoginBase.LoginType.QQ:
                {
                    //获取 access_token
                    var tokenEntity = QQ.AccessToken(new QQ_AccessToken_RequestEntity()
                        {
                            code = code
                        });

                    //获取 OpendId
                    var openidEntity = QQ.OpenId(new QQ_OpenId_RequestEntity()
                        {
                            access_token = tokenEntity.access_token
                        });

                    //获取 UserInfo
                    var userEntity = QQ.OpenId_Get_User_Info(new QQ_OpenAPI_RequestEntity()
                        {
                            access_token = tokenEntity.access_token,
                            openid       = openidEntity.openid
                        });

                    //身份唯一标识
                    openId = openidEntity.openid;
                }
                break;

                case LoginBase.LoginType.WeiBo:
                {
                    //获取 access_token
                    var tokenEntity = Weibo.AccessToken(new Weibo_AccessToken_RequestEntity()
                        {
                            code = code
                        });

                    //获取 access_token 的授权信息
                    var tokenInfoEntity = Weibo.GetTokenInfo(new Weibo_GetTokenInfo_RequestEntity()
                        {
                            access_token = tokenEntity.access_token
                        });

                    //获取 users/show
                    var userEntity = Weibo.UserShow(new Weibo_UserShow_RequestEntity()
                        {
                            access_token = tokenEntity.access_token,
                            uid          = Convert.ToInt64(tokenInfoEntity.uid)
                        });

                    openId = tokenEntity.access_token;
                }
                break;

                case LoginBase.LoginType.WeChat:
                {
                    //获取 access_token
                    var tokenEntity = WeChat.AccessToken(new WeChat_AccessToken_RequestEntity()
                        {
                            code = code
                        });

                    //获取 user
                    var userEntity = WeChat.Get_User_Info(new WeChat_OpenAPI_RequestEntity()
                        {
                            access_token = tokenEntity.access_token,
                            openid       = tokenEntity.openid
                        });

                    //身份唯一标识
                    openId = tokenEntity.openid;
                }
                break;

                case LoginBase.LoginType.GitHub:
                {
                    //申请的应用名称,非常重要
                    GitHubConfig.ApplicationName = "netnrf";

                    //获取 access_token
                    var tokenEntity = GitHub.AccessToken(new GitHub_AccessToken_RequestEntity()
                        {
                            code = code
                        });

                    //获取 user
                    var userEntity = GitHub.User(new GitHub_User_RequestEntity()
                        {
                            access_token = tokenEntity.access_token
                        });

                    openId = userEntity.id.ToString();
                }
                break;

                case LoginBase.LoginType.TaoBao:
                {
                    //获取 access_token
                    var tokenEntity = Taobao.AccessToken(new Taobao_AccessToken_RequestEntity()
                        {
                            code = code
                        });

                    openId = tokenEntity.open_uid;
                }
                break;

                case LoginBase.LoginType.MicroSoft:
                {
                    //获取 access_token
                    var tokenEntity = MicroSoft.AccessToken(new MicroSoft_AccessToken_RequestEntity()
                        {
                            code = code
                        });

                    //获取 user
                    var userEntity = MicroSoft.User(new MicroSoft_User_RequestEntity()
                        {
                            access_token = tokenEntity.access_token
                        });

                    openId = userEntity.id.ToString();
                }
                break;
                }

                //拿到登录标识
                if (string.IsNullOrWhiteSpace(openId))
                {
                    //TO DO
                }
            }
        }
Exemplo n.º 5
0
            /// <summary>
            /// 回调方法
            /// </summary>
            /// <param name="authorizeResult">接收授权码、防伪参数</param>
            public void AuthCallback(LoginBase.AuthorizeResult authorizeResult)
            {
                if (string.IsNullOrWhiteSpace(authorizeResult.code))
                {
                    //打开链接没登录授权
                }
                else
                {
                    //唯一标示
                    string OpenId = string.Empty;

                    switch (loginType)
                    {
                    case LoginBase.LoginType.QQ:
                    {
                        //获取 access_token
                        var tokenEntity = QQ.AccessToken(new QQ_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        //获取 OpendId
                        var openidEntity = QQ.OpenId(new QQ_OpenId_RequestEntity()
                            {
                                access_token = tokenEntity.access_token
                            });

                        //获取 UserInfo
                        _ = QQ.OpenId_Get_User_Info(new QQ_OpenAPI_RequestEntity()
                            {
                                access_token = tokenEntity.access_token,
                                openid       = openidEntity.openid
                            });

                        //身份唯一标识
                        OpenId = openidEntity.openid;
                    }
                    break;

                    case LoginBase.LoginType.WeiBo:
                    {
                        //获取 access_token
                        var tokenEntity = Weibo.AccessToken(new Weibo_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        //获取 access_token 的授权信息
                        var tokenInfoEntity = Weibo.GetTokenInfo(new Weibo_GetTokenInfo_RequestEntity()
                            {
                                access_token = tokenEntity.access_token
                            });

                        //获取 users/show
                        _ = Weibo.UserShow(new Weibo_UserShow_RequestEntity()
                            {
                                access_token = tokenEntity.access_token,
                                uid          = Convert.ToInt64(tokenInfoEntity.uid)
                            });

                        OpenId = tokenEntity.access_token;
                    }
                    break;

                    case LoginBase.LoginType.WeChat:
                    {
                        //获取 access_token
                        var tokenEntity = WeChat.AccessToken(new WeChat_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        //获取 user
                        _ = WeChat.Get_User_Info(new WeChat_OpenAPI_RequestEntity()
                            {
                                access_token = tokenEntity.access_token,
                                openid       = tokenEntity.openid
                            });

                        //身份唯一标识
                        OpenId = tokenEntity.openid;
                    }
                    break;

                    case LoginBase.LoginType.GitHub:
                    {
                        //获取 access_token
                        var tokenEntity = GitHub.AccessToken(new GitHub_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        //获取 user
                        var userEntity = GitHub.User(new GitHub_User_RequestEntity()
                            {
                                access_token = tokenEntity.access_token
                            });

                        OpenId = userEntity.id.ToString();
                    }
                    break;

                    case LoginBase.LoginType.TaoBao:
                    {
                        //获取 access_token
                        var tokenEntity = TaoBao.AccessToken(new TaoBao_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        OpenId = tokenEntity.open_uid;
                    }
                    break;

                    case LoginBase.LoginType.MicroSoft:
                    {
                        //获取 access_token
                        var tokenEntity = MicroSoft.AccessToken(new MicroSoft_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        //获取 user
                        var userEntity = MicroSoft.User(new MicroSoft_User_RequestEntity()
                            {
                                access_token = tokenEntity.access_token
                            });

                        OpenId = userEntity.id.ToString();
                    }
                    break;

                    case LoginBase.LoginType.DingTalk:
                    {
                        //获取 user
                        var userEntity = DingTalk.User(new DingTalk_User_RequestEntity(), authorizeResult.code);

                        OpenId = userEntity?.openid;
                    }
                    break;

                    case LoginBase.LoginType.Gitee:
                    {
                        //获取 access_token
                        var tokenEntity = Gitee.AccessToken(new Gitee_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });

                        //获取 user
                        var userEntity = Gitee.User(new Gitee_User_RequestEntity()
                            {
                                access_token = tokenEntity.access_token
                            });

                        OpenId = userEntity.id.ToString();
                    }
                    break;
                    }

                    //拿到登录标识
                    if (string.IsNullOrWhiteSpace(OpenId))
                    {
                        //TO DO
                    }
                }
            }
Exemplo n.º 6
0
        /// <summary>
        /// 登录授权回调
        /// </summary>
        /// <param name="authorizeResult">获取授权码以及防伪标识</param>
        /// <returns></returns>
        public IActionResult AuthCallback(LoginBase.AuthorizeResult authorizeResult)
        {
            var vm = new SharedResultVM();

            try
            {
                if (string.IsNullOrWhiteSpace(authorizeResult.code))
                {
                    vm.Set(SharedEnum.RTag.unauthorized);
                }
                else
                {
                    //唯一标示
                    string openId = string.Empty;
                    //注册信息
                    var mo = new Domain.UserInfo()
                    {
                        LoginLimit     = 0,
                        UserSex        = 0,
                        UserCreateTime = DateTime.Now
                    };
                    //头像
                    string avatar = string.Empty;

                    Enum.TryParse(RouteData.Values["id"]?.ToString(), true, out LoginBase.LoginType vtype);

                    switch (vtype)
                    {
                    case LoginBase.LoginType.QQ:
                    {
                        //获取 access_token
                        var tokenEntity = QQ.AccessToken(new QQ_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });
                        Console.WriteLine(tokenEntity.ToJson());

                        //获取 OpendId
                        var openidEntity = QQ.OpenId(tokenEntity.access_token);
                        Console.WriteLine(openidEntity.ToJson());

                        //获取 UserInfo
                        var userEntity = QQ.OpenId_Get_User_Info(new QQ_OpenAPI_RequestEntity()
                            {
                                access_token = tokenEntity.access_token,
                                openid       = openidEntity.openid
                            });
                        Console.WriteLine(userEntity.ToJson());

                        //身份唯一标识
                        openId     = openidEntity.openid;
                        mo.OpenId1 = openId;

                        mo.Nickname = userEntity.nickname;
                        mo.UserSex  = userEntity.gender == "男" ? 1 : 2;
                        mo.UserSay  = "";
                        mo.UserUrl  = "";

                        avatar = userEntity.figureurl_2;
                    }
                    break;

                    case LoginBase.LoginType.WeiBo:
                    {
                        //获取 access_token
                        var tokenEntity = Weibo.AccessToken(new Weibo_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });
                        Console.WriteLine(tokenEntity.ToJson());

                        //获取 access_token 的授权信息
                        var tokenInfoEntity = Weibo.GetTokenInfo(tokenEntity.access_token);
                        Console.WriteLine(tokenInfoEntity.ToJson());

                        //获取 users/show
                        var userEntity = Weibo.UserShow(new Weibo_UserShow_RequestEntity()
                            {
                                access_token = tokenEntity.access_token,
                                uid          = Convert.ToInt64(tokenInfoEntity.uid)
                            });
                        Console.WriteLine(userEntity.ToJson());

                        openId     = tokenEntity.access_token;
                        mo.OpenId2 = openId;

                        mo.Nickname = userEntity.screen_name;
                        mo.UserSex  = userEntity.gender == "m" ? 1 : userEntity.gender == "f" ? 2 : 0;
                        mo.UserSay  = userEntity.description;
                        mo.UserUrl  = userEntity.domain;

                        avatar = userEntity.avatar_large;
                    }
                    break;

                    case LoginBase.LoginType.WeChat:
                    {
                        //获取 access_token
                        var tokenEntity = Netnr.Login.WeChat.AccessToken(new WeChat_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });
                        Console.WriteLine(tokenEntity.ToJson());

                        //openId = tokenEntity.openid;

                        //获取 user
                        var userEntity = Netnr.Login.WeChat.Get_User_Info(new WeChat_OpenAPI_RequestEntity()
                            {
                                access_token = tokenEntity.access_token,
                                openid       = tokenEntity.openid
                            });
                        Console.WriteLine(userEntity.ToJson());

                        avatar = userEntity.headimgurl;
                    }
                    break;

                    case LoginBase.LoginType.GitHub:
                    {
                        //获取 access_token
                        var tokenEntity = GitHub.AccessToken(new GitHub_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });
                        Console.WriteLine(tokenEntity.ToJson());

                        //获取 user
                        var userEntity = GitHub.User(tokenEntity.access_token);
                        Console.WriteLine(userEntity.ToJson());

                        openId     = userEntity.id.ToString();
                        mo.OpenId3 = openId;

                        mo.Nickname = userEntity.name;
                        mo.UserSay  = userEntity.bio;
                        mo.UserUrl  = userEntity.blog;
                        mo.UserMail = userEntity.email;

                        avatar = userEntity.avatar_url;
                    }
                    break;

                    case LoginBase.LoginType.Gitee:
                    {
                        //获取 access_token
                        var tokenEntity = Gitee.AccessToken(new Gitee_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });
                        Console.WriteLine(tokenEntity.ToJson());

                        //获取 user
                        var userEntity = Gitee.User(tokenEntity.access_token);
                        Console.WriteLine(userEntity.ToJson());

                        //openId = userEntity.id.ToString();

                        mo.Nickname = userEntity.name;
                        mo.UserSay  = userEntity.bio;
                        mo.UserUrl  = userEntity.blog;

                        avatar = userEntity.avatar_url;
                    }
                    break;

                    case LoginBase.LoginType.TaoBao:
                    {
                        //获取 access_token
                        var tokenEntity = TaoBao.AccessToken(new TaoBao_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });
                        Console.WriteLine(tokenEntity.ToJson());

                        openId     = tokenEntity.open_uid;
                        mo.OpenId4 = openId;

                        mo.Nickname = "淘宝用户";
                    }
                    break;

                    case LoginBase.LoginType.MicroSoft:
                    {
                        //获取 access_token
                        var tokenEntity = MicroSoft.AccessToken(new MicroSoft_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });
                        Console.WriteLine(tokenEntity.ToJson());

                        //获取 user
                        var userEntity = MicroSoft.User(tokenEntity.access_token);
                        Console.WriteLine(userEntity.ToJson());

                        openId     = userEntity.id;
                        mo.OpenId5 = openId;

                        mo.Nickname = userEntity.last_name + userEntity.first_name;
                        mo.UserMail = userEntity.emails?["account"].ToStringOrEmpty();
                    }
                    break;

                    case LoginBase.LoginType.DingTalk:
                    {
                        //获取 user
                        var userEntity = DingTalk.User(new DingTalk_User_RequestEntity(), authorizeResult.code);
                        Console.WriteLine(userEntity.ToJson());

                        openId     = userEntity.openid;
                        mo.OpenId6 = openId;

                        mo.Nickname = userEntity.nick;
                    }
                    break;

                    case LoginBase.LoginType.Google:
                    {
                        //获取 access_token
                        var tokenEntity = Google.AccessToken(new Google_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });
                        Console.WriteLine(tokenEntity.ToJson());

                        //获取 user
                        var userEntity = Google.User(tokenEntity.access_token);
                        Console.WriteLine(userEntity.ToJson());

                        //openId = userEntity.sub;

                        avatar = userEntity.picture;
                    }
                    break;

                    case LoginBase.LoginType.AliPay:
                    {
                        //获取 access_token
                        var tokenEntity = AliPay.AccessToken(new AliPay_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });
                        Console.WriteLine(tokenEntity.ToJson());

                        //openId = tokenEntity.user_id;

                        //获取 user
                        var userEntity = AliPay.User(new AliPay_User_RequestEntity()
                            {
                                auth_token = tokenEntity.access_token
                            });
                        Console.WriteLine(userEntity.ToJson());

                        avatar = userEntity.avatar;
                    }
                    break;

                    case LoginBase.LoginType.StackOverflow:
                    {
                        //获取 access_token
                        var tokenEntity = StackOverflow.AccessToken(new StackOverflow_AccessToken_RequestEntity()
                            {
                                code = authorizeResult.code
                            });
                        Console.WriteLine(tokenEntity.ToJson());

                        //获取 user
                        var userEntity = StackOverflow.User(new StackOverflow_User_RequestEntity()
                            {
                                access_token = tokenEntity.access_token
                            });
                        Console.WriteLine(userEntity.ToJson());

                        //openId= userEntity.user_id;

                        avatar = userEntity.profile_image;
                    }
                    break;
                    }

                    mo.UserCreateTime = DateTime.Now;
                    mo.UserName       = openId;
                    mo.UserPwd        = CalcTo.MD5(openId);
                    if (!string.IsNullOrWhiteSpace(avatar))
                    {
                        mo.UserPhoto = UniqueTo.LongId().ToString() + ".jpg";
                    }
                    Console.WriteLine(mo.ToJson());

                    if (string.IsNullOrWhiteSpace(openId))
                    {
                        vm.Set(SharedEnum.RTag.unauthorized);
                        vm.Msg = "身份验证失败";
                    }
                    else
                    {
                        //判断是绑定操作
                        bool isbind = User.Identity.IsAuthenticated && authorizeResult.state.StartsWith("bind");
                        if (isbind)
                        {
                            int uid = Apps.LoginService.Get(HttpContext).UserId;

                            //检测是否绑定其它账号
                            var queryIsBind = db.UserInfo.Where(x => x.UserId != uid);
                            switch (vtype)
                            {
                            case LoginBase.LoginType.QQ:
                                queryIsBind = queryIsBind.Where(x => x.OpenId1 == openId);
                                break;

                            case LoginBase.LoginType.WeiBo:
                                queryIsBind = queryIsBind.Where(x => x.OpenId2 == openId);
                                break;

                            case LoginBase.LoginType.GitHub:
                                queryIsBind = queryIsBind.Where(x => x.OpenId3 == openId);
                                break;

                            case LoginBase.LoginType.TaoBao:
                                queryIsBind = queryIsBind.Where(x => x.OpenId4 == openId);
                                break;

                            case LoginBase.LoginType.MicroSoft:
                                queryIsBind = queryIsBind.Where(x => x.OpenId5 == openId);
                                break;

                            case LoginBase.LoginType.DingTalk:
                                queryIsBind = queryIsBind.Where(x => x.OpenId6 == openId);
                                break;
                            }
                            if (queryIsBind.Any())
                            {
                                return(Content("已绑定其它账号,不能重复绑定"));
                            }

                            var userInfo = db.UserInfo.Find(uid);

                            switch (vtype)
                            {
                            case LoginBase.LoginType.QQ:
                                userInfo.OpenId1 = openId;
                                break;

                            case LoginBase.LoginType.WeiBo:
                                userInfo.OpenId2 = openId;
                                break;

                            case LoginBase.LoginType.GitHub:
                                userInfo.OpenId3 = openId;
                                break;

                            case LoginBase.LoginType.TaoBao:
                                userInfo.OpenId4 = openId;
                                break;

                            case LoginBase.LoginType.MicroSoft:
                                userInfo.OpenId5 = openId;
                                break;

                            case LoginBase.LoginType.DingTalk:
                                userInfo.OpenId6 = openId;
                                break;
                            }
                            db.UserInfo.Update(userInfo);
                            db.SaveChanges();

                            return(Redirect("/user/setting"));
                        }
                        else
                        {
                            Domain.UserInfo vmo = null;
                            switch (vtype)
                            {
                            case LoginBase.LoginType.QQ:
                                vmo = db.UserInfo.FirstOrDefault(x => x.OpenId1 == openId);
                                break;

                            case LoginBase.LoginType.WeiBo:
                                vmo = db.UserInfo.FirstOrDefault(x => x.OpenId2 == openId);
                                break;

                            case LoginBase.LoginType.GitHub:
                                vmo = db.UserInfo.FirstOrDefault(x => x.OpenId3 == openId);
                                break;

                            case LoginBase.LoginType.TaoBao:
                                vmo = db.UserInfo.FirstOrDefault(x => x.OpenId4 == openId);
                                break;

                            case LoginBase.LoginType.MicroSoft:
                                vmo = db.UserInfo.FirstOrDefault(x => x.OpenId5 == openId);
                                break;

                            case LoginBase.LoginType.DingTalk:
                                vmo = db.UserInfo.FirstOrDefault(x => x.OpenId6 == openId);
                                break;
                            }
                            //未注册
                            if (vmo == null)
                            {
                                var ruvm = RegisterUser(mo);
                                if (ruvm.Code == 200)
                                {
                                    vm = ValidateLogin(vtype, mo);
                                    //拉取头像
                                    if (vm.Code == 200 && !string.IsNullOrWhiteSpace(avatar))
                                    {
                                        try
                                        {
                                            //物理根路径
                                            var prp   = GlobalTo.GetValue("StaticResource:PhysicalRootPath").Replace("~", GlobalTo.ContentRootPath);
                                            var ppath = PathTo.Combine(prp, GlobalTo.GetValue("StaticResource:AvatarPath"));

                                            if (!Directory.Exists(ppath))
                                            {
                                                Directory.CreateDirectory(ppath);
                                            }

                                            HttpTo.DownloadSave(HttpTo.HWRequest(avatar), PathTo.Combine(ppath, mo.UserPhoto));
                                        }
                                        catch (Exception ex)
                                        {
                                            Console.WriteLine(ex);
                                        }
                                    }
                                }
                                else
                                {
                                    vm.Msg = ruvm.Msg;
                                }
                            }
                            else
                            {
                                vm = ValidateLogin(vtype, vmo);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Apps.FilterConfigs.WriteLog(HttpContext, ex);
                Response.Headers["X-Output-Msg"] = ex.ToJson();
                vm.Set(ex);
            }

            //成功
            if (vm.Code == 200)
            {
                var rurl = Request.Cookies["ReturnUrl"];
                rurl = string.IsNullOrWhiteSpace(rurl) ? "/" : rurl;

                if (rurl.StartsWith("http"))
                {
                    rurl += "?cookie=ok";
                }

                return(Redirect(rurl));
            }
            else
            {
                return(Redirect("/home/error"));
            }
        }