예제 #1
0
        /// <summary>
        /// 验证Token
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public static UserInfo TokenValid(string token)
        {
            UserInfo mo = null;

            try
            {
                var key = GlobalTo.GetValue("VerifyCode:Key");

                var jo = CalcTo.DeDES(token, key).ToJObject();

                if (DateTime.Now.ToTimestamp() < long.Parse(jo["expired"].ToString()))
                {
                    mo = jo["mo"].ToString().ToEntity <UserInfo>();
                }
            }
            catch (Exception)
            {
            }

            return(mo);
        }
예제 #2
0
        /// <summary>
        /// 验证
        /// </summary>
        /// <returns></returns>
        public IActionResult Verify()
        {
            var vm = new ActionResultVM();

            var id = RouteData.Values["id"]?.ToString().ToUpper();

            if (!string.IsNullOrWhiteSpace(id))
            {
                var uinfo = new UserAuthAid(HttpContext).Get();

                switch (id.ToLower())
                {
                //发送验证邮箱
                case "send":
                {
                    if (User.Identity.IsAuthenticated)
                    {
                        using var db = new ContextBase();
                        var usermo = db.UserInfo.Find(uinfo.UserId);
                        if (usermo.UserMailValid == 1)
                        {
                            vm.msg = "邮箱已经完成验证";
                        }
                        else if (string.IsNullOrWhiteSpace(usermo.UserMail))
                        {
                            vm.msg = "邮箱不能为空";
                        }
                        else
                        {
                            var cacheKey = "Global_VerifyMail_" + usermo.UserMail;
                            var issend   = CacheTo.Get(cacheKey) as bool?;
                            if (issend == true)
                            {
                                vm.msg = "1分钟内只能发送一次验证信息";
                            }
                            else
                            {
                                var tml = FileTo.ReadText(GlobalTo.WebRootPath + "/lib/mailchecker/", "list.txt");
                                if (tml.Contains(usermo.UserMail.Split('@').LastOrDefault()))
                                {
                                    vm.msg = "该邮箱已被屏蔽";
                                }
                                else
                                {
                                    //发送验证

                                    var ToMail = usermo.UserMail;

                                    var vjson = new
                                    {
                                        mail = ToMail,
                                        ts   = DateTime.Now.ToTimestamp()
                                    }.ToJson();
                                    var vcode = CalcTo.EnDES(vjson, GlobalTo.GetValue("VerifyCode:Key")).ToLower();

                                    var VerifyLink = string.Format(GlobalTo.GetValue("VerifyCode:Url"), vcode);

                                    var txt = FileTo.ReadText(GlobalTo.WebRootPath + "/template/", "sendmailverify.html");
                                    txt = txt.Replace("@ToMail@", ToMail).Replace("@VerifyLink@", VerifyLink);

                                    vm = MailAid.Send(ToMail, "验证你的邮箱", txt);

                                    if (vm.code == 200)
                                    {
                                        vm.msg = "已发送成功";
                                        CacheTo.Set(cacheKey, true, 60, false);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        vm.msg = "请登录";
                    }
                }
                break;

                //验证邮箱
                default:
                    try
                    {
                        var vjson = CalcTo.DeDES(id, GlobalTo.GetValue("VerifyCode:Key")).ToJObject();
                        if (DateTime.Now.ToTimestamp() - Convert.ToInt32(vjson["ts"]) < 60 * 5)
                        {
                            var mail = vjson["mail"].ToString();
                            if (string.IsNullOrWhiteSpace(mail))
                            {
                                vm.msg = "邮件地址有误";
                            }
                            else
                            {
                                using var db = new ContextBase();
                                var usermo = db.UserInfo.FirstOrDefault(x => x.UserMail == mail);
                                if (usermo != null)
                                {
                                    if (usermo.UserMailValid == 1)
                                    {
                                        vm.msg = "已验证,勿重复验证";
                                    }
                                    else
                                    {
                                        usermo.UserMailValid = 1;

                                        db.UserInfo.Update(usermo);

                                        int num = db.SaveChanges();

                                        vm.Set(num > 0);
                                        if (vm.code == 200)
                                        {
                                            vm.msg = "恭喜你,验证成功";
                                        }
                                    }
                                }
                                else
                                {
                                    vm.msg = "邮件地址无效";
                                }
                            }
                        }
                        else
                        {
                            vm.msg = "链接已过期(5分钟内有效)";
                        }
                    }
                    catch (Exception)
                    {
                        vm.msg = "链接已失效";
                    }
                    break;
                }
            }
            else
            {
                vm.msg = "缺失验证码信息";
            }

            return(View(vm));
        }