예제 #1
0
        public ActionResultVM UpdatePassword(string oldpwd, string newpwd)
        {
            var vm = new ActionResultVM();

            int uid = new UserAuthAid(HttpContext).Get().UserId;

            using (var db = new ContextBase())
            {
                var userinfo = db.UserInfo.Find(uid);
                if (userinfo.UserPwd == CalcTo.MD5(oldpwd))
                {
                    userinfo.UserPwd = CalcTo.MD5(newpwd);
                    db.UserInfo.Update(userinfo);
                    var num = db.SaveChanges();

                    vm.Set(num > 0);
                }
                else
                {
                    vm.Set(ARTag.unauthorized);
                }
            };

            return(vm);
        }
예제 #2
0
        public SharedResultVM ReplyList(string id, int page = 1)
        {
            return(SharedResultVM.Try(vm =>
            {
                var uinfo = Apps.LoginService.Get(HttpContext);

                var pag = new SharedPaginationVM
                {
                    PageNumber = Math.Max(page, 1),
                    PageSize = 10
                };

                var list = Application.CommonService.ReplyOneQuery(Application.EnumService.ReplyType.GuffRecord, id, pag);
                //匿名用户,生成邮箱MD5加密用于请求头像
                foreach (var item in list)
                {
                    if (item.Uid == 0 && !string.IsNullOrWhiteSpace(item.UrAnonymousMail))
                    {
                        item.Spare3 = CalcTo.MD5(item.UrAnonymousMail);
                    }
                }

                var pvm = new SharedPageVM()
                {
                    Rows = list,
                    Pag = pag
                };
                vm.Data = pvm;

                vm.Set(SharedEnum.RTag.success);

                return vm;
            }));
        }
예제 #3
0
        public IActionResult Register(UserInfo mo, string RegisterCode)
        {
            var vm = new ActionResultVM();

            if (string.IsNullOrWhiteSpace(RegisterCode) || HttpContext.Session.GetString("RegisterCode") != RegisterCode)
            {
                vm.msg = "验证码错误或已过期";
            }
            else if (!(mo.UserName?.Length >= 5 && mo.UserPwd?.Length >= 5))
            {
                vm.msg = "账号、密码长度至少 5 位数";
            }
            else
            {
                mo.UserPwd        = CalcTo.MD5(mo.UserPwd);
                mo.UserCreateTime = DateTime.Now;

                //邮箱注册
                if (ParsingTo.IsMail(mo.UserName))
                {
                    mo.UserMail = mo.UserName;
                }
                vm = RegisterUser(mo);
            }

            ViewData["UserName"] = mo.UserName;

            return(View(vm));
        }
예제 #4
0
        public ActionResultVM ReplyList(string id, int page = 1)
        {
            var vm = new ActionResultVM();

            try
            {
                var uinfo = new UserAuthAid(HttpContext).Get();

                var pag = new PaginationVM
                {
                    PageNumber = Math.Max(page, 1),
                    PageSize   = 10
                };

                var list = Func.Common.ReplyOneQuery(EnumAid.ReplyType.GuffRecord, id, pag);
                //匿名用户,生成邮箱MD5加密用于请求头像
                foreach (var item in list)
                {
                    if (item.Uid == 0 && !string.IsNullOrWhiteSpace(item.UrAnonymousMail))
                    {
                        item.Spare3 = CalcTo.MD5(item.UrAnonymousMail);
                    }
                }

                var pvm = new PageVM()
                {
                    Rows = list,
                    Pag  = pag
                };
                vm.data = pvm;

                vm.Set(ARTag.success);
            }
            catch (Exception ex)
            {
                vm.Set(ex);
                ConsoleTo.Log(ex);
            }

            return(vm);
        }
예제 #5
0
        /// <summary>
        /// 登录授权回调
        /// </summary>
        /// <param name="authorizeResult">获取授权码以及防伪标识</param>
        /// <returns></returns>
        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 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        = CalcTo.MD5(openId);
                    if (!string.IsNullOrWhiteSpace(avatar))
                    {
                        mo.UserPhoto = 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 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();
                            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);

                ConsoleTo.Log(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
            {
                string msg = "【登录失败】( " + vm.msg + " )".ToEncode();
                return(Redirect("/home/error?msg=" + msg));
            }
        }
예제 #6
0
        /// <summary>
        /// 公共登录验证
        /// </summary>
        /// <param name="vt">登录类型</param>
        /// <param name="mo">用户信息</param>
        /// <param name="isremember">记住账号</param>
        /// <returns></returns>
        private ActionResultVM ValidateLogin(ValidateloginType vt, UserInfo mo, bool isremember = true)
        {
            var vm = new ActionResultVM();

            string sql = string.Empty;

            using var db = new ContextBase();
            var      uiR   = db.UserInfo;
            UserInfo outMo = new UserInfo();

            switch (vt)
            {
            case ValidateloginType.local:
                if (string.IsNullOrWhiteSpace(mo.UserName) || string.IsNullOrWhiteSpace(mo.UserPwd))
                {
                    vm.msg = "用户名或密码不能为空";
                    return(vm);
                }
                else
                {
                    mo.UserPwd = CalcTo.MD5(mo.UserPwd);

                    //邮箱登录
                    if (ParsingTo.IsMail(mo.UserName))
                    {
                        outMo = uiR.FirstOrDefault(x => x.UserMail == mo.UserName && x.UserPwd == mo.UserPwd);
                    }
                    else
                    {
                        outMo = uiR.FirstOrDefault(x => x.UserName == mo.UserName && x.UserPwd == mo.UserPwd);
                    }
                }
                break;

            case ValidateloginType.qq:
                outMo = uiR.FirstOrDefault(x => x.OpenId1.Equals(mo.OpenId1));
                break;

            case ValidateloginType.weibo:
                outMo = uiR.FirstOrDefault(x => x.OpenId2.Equals(mo.OpenId2));
                break;

            case ValidateloginType.github:
                outMo = uiR.FirstOrDefault(x => x.OpenId3.Equals(mo.OpenId3));
                break;

            case ValidateloginType.taobao:
                outMo = uiR.FirstOrDefault(x => x.OpenId4.Equals(mo.OpenId4));
                break;

            case ValidateloginType.microsoft:
                outMo = uiR.FirstOrDefault(x => x.OpenId5.Equals(mo.OpenId5));
                break;

            case ValidateloginType.dingtalk:
                outMo = uiR.FirstOrDefault(x => x.OpenId6.Equals(mo.OpenId6));
                break;
            }

            if (outMo == null || outMo.UserId == 0)
            {
                vm.msg = "用户名或密码错误";
                return(vm);
            }

            if (outMo.LoginLimit == 1)
            {
                vm.msg = "用户已被禁止登录";
                return(vm);
            }

            //刷新登录标记
            outMo.UserLoginTime = DateTime.Now;
            outMo.UserSign      = outMo.UserLoginTime.Value.ToTimestamp().ToString();
            uiR.Update(outMo);
            var num = db.SaveChanges();

            if (num < 1)
            {
                vm.msg = "请求登录被拒绝";
                return(vm);
            }

            try
            {
                //登录标记 缓存5分钟,绝对过期
                var usk = "UserSign_" + outMo.UserId;
                CacheTo.Set(usk, outMo.UserSign, 5 * 60, false);

                //写入授权
                SetAuth(HttpContext, outMo, isremember);

                //生成Token
                vm.data = UserAuthAid.TokenMake(outMo);

                vm.Set(ARTag.success);
            }
            catch (Exception ex)
            {
                vm.Set(ex);
            }

            return(vm);
        }
예제 #7
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"));
            }
        }
예제 #8
0
        /// <summary>
        /// 公共登录验证
        /// </summary>
        /// <param name="vt">登录类型</param>
        /// <param name="mo">用户信息</param>
        /// <param name="isremember">记住账号</param>
        /// <returns></returns>
        private SharedResultVM ValidateLogin(LoginBase.LoginType?vt, Domain.UserInfo mo, bool isremember = true)
        {
            var vm = new SharedResultVM();

            string sql = string.Empty;

            var uiR = db.UserInfo;

            Domain.UserInfo outMo = new();

            switch (vt)
            {
            case LoginBase.LoginType.QQ:
                outMo = uiR.FirstOrDefault(x => x.OpenId1.Equals(mo.OpenId1));
                break;

            case LoginBase.LoginType.WeiBo:
                outMo = uiR.FirstOrDefault(x => x.OpenId2.Equals(mo.OpenId2));
                break;

            case LoginBase.LoginType.GitHub:
                outMo = uiR.FirstOrDefault(x => x.OpenId3.Equals(mo.OpenId3));
                break;

            case LoginBase.LoginType.TaoBao:
                outMo = uiR.FirstOrDefault(x => x.OpenId4.Equals(mo.OpenId4));
                break;

            case LoginBase.LoginType.MicroSoft:
                outMo = uiR.FirstOrDefault(x => x.OpenId5.Equals(mo.OpenId5));
                break;

            case LoginBase.LoginType.DingTalk:
                outMo = uiR.FirstOrDefault(x => x.OpenId6.Equals(mo.OpenId6));
                break;

            default:
                if (string.IsNullOrWhiteSpace(mo.UserName) || string.IsNullOrWhiteSpace(mo.UserPwd))
                {
                    vm.Msg = "用户名或密码不能为空";
                    return(vm);
                }
                else
                {
                    mo.UserPwd = CalcTo.MD5(mo.UserPwd);

                    //邮箱登录
                    if (ParsingTo.IsMail(mo.UserName))
                    {
                        outMo = uiR.FirstOrDefault(x => x.UserMail == mo.UserName && x.UserPwd == mo.UserPwd);
                    }
                    else
                    {
                        outMo = uiR.FirstOrDefault(x => x.UserName == mo.UserName && x.UserPwd == mo.UserPwd);
                    }
                }
                break;
            }

            if (outMo == null || outMo.UserId == 0)
            {
                vm.Msg = "用户名或密码错误";
                return(vm);
            }

            if (outMo.LoginLimit == 1)
            {
                vm.Msg = "用户已被禁止登录";
                return(vm);
            }

            try
            {
                //刷新登录标记
                outMo.UserLoginTime = DateTime.Now;
                outMo.UserSign      = outMo.UserLoginTime.Value.ToTimestamp().ToString();
                uiR.Update(outMo);
                db.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            try
            {
                //登录标记 缓存5分钟,绝对过期
                if (GlobalTo.GetValue <bool>("Common:SingleSignOn"))
                {
                    var usk = "UserSign_" + outMo.UserId;
                    CacheTo.Set(usk, outMo.UserSign, 5 * 60, false);
                }

                //写入授权
                SetAuth(HttpContext, outMo, isremember);

                //生成Token
                vm.Data = Apps.LoginService.TokenMake(outMo);

                vm.Set(SharedEnum.RTag.success);
            }
            catch (Exception ex)
            {
                vm.Set(ex);
            }

            return(vm);
        }