示例#1
0
        public AjaxResult SmsCode(string email)
        {
            AjaxResult amm;
            int        limitcount   = 10;
            int        limitMinutes = 10;

            if (!Validate.IsEmail(email))
            {
                return(AjaxResult.Error("邮箱格式不正确"));
            }

            AppUser userEntity = this.Queryable(u => u.Email == email && u.DeleteMark == false).FirstOrDefault();

            if (userEntity == null)
            {
                amm = AjaxResult.Error("此邮箱尚未注册!");
            }
            else
            {
                string count = _iRedisHelper.StringGet <string>(email);
                //缓存十分钟,如果缓存中存在,且请求次数超过10次,则返回
                if (!string.IsNullOrEmpty(count) && Convert.ToInt32(count) >= limitcount)
                {
                    amm = AjaxResult.Error("没收到邮箱:请联系[email protected]");
                }
                else
                {
                    #region 发送邮箱,并写入缓存,更新登录信息表的token,date,code
                    int num = 0;
                    if (!string.IsNullOrEmpty(count))
                    {
                        num = Convert.ToInt32(count);
                    }
                    string countplus1 = num + 1 + "";
                    _iRedisHelper.StringSet <string>(email, countplus1, new TimeSpan(0, limitMinutes, 0));

                    ResetPwd resetpwdEntry = new ResetPwd
                    {
                        UserId         = (int)userEntity.Id,
                        ModifyPwdToken = Utils.GuId(),
                        PwdCodeTme     = DateTime.Now,
                        ModfiyPwdCode  = Utils.RndNum(6),
                        LoginName      = userEntity.LoginName,
                        NickName       = userEntity.NickName
                    };

                    //将发送验证码的数据存入redis缓存中

                    _iRedisHelper.StringSet(email + "sendcodekey", resetpwdEntry, new TimeSpan(0, limitMinutes, 0));
                    //发送找回密码的邮件

                    string body = UiHelper.FormatEmail(resetpwdEntry, "PwdReSetTemplate");
                    _imailHelper.SendByThread(email, "[、天上有木月博客] 密码找回", body);

                    #endregion
                    //将修改密码的token返回给前端
                    amm = AjaxResult.Info("验证码已发送至你的邮箱!", resetpwdEntry.ModifyPwdToken, ResultType.Success.ToString());
                }
            }
            return(amm);
        }
示例#2
0
        public ActionResult Add(GuestBook viewModel)
        {
            if (viewModel.AuEmail.IsNullOrEmpty())
            {
                return(Error("Email不能为空!"));
            }
            else if (!Validate.IsEmail(viewModel.AuEmail))
            {
                return(Error("邮箱格式不正确!"));
            }
            if (viewModel.AuName.IsNullOrEmpty())
            {
                return(Error("用户昵称不能为空!"));
            }
            viewModel.Ip = Net.Ip;

            string userIp = RedisHelper.StringGet(string.Format(ConstHelper.GuestBook, "IP-" + viewModel.Ip));

            if (userIp.IsNotNull())
            {
                return(Error("您的留言太频繁了,请稍后再试!!!"));
            }

            if (!ModelState.IsValid)
            {
                return(Error(ModelState.Values.FirstOrDefault(u => u.Errors.Count > 0)?.Errors[0].ErrorMessage));
            }
            OperatorProvider op = OperatorProvider.Provider;

            //未登录状态下,将生成一个随机头像
            if (op.CurrentUser == null)
            {
                viewModel.Avatar = "/Content/user/" + new Random(DateTime.Now.Second).Next(1, 361) + ".png";
            }
            else
            {
            }

            viewModel.Create();

            viewModel.System      = Net.GetOsNameByUserAgent(Request.UserAgent);
            viewModel.UserHost    = Net.Host;
            viewModel.GeoPosition = Net.GetLocation(viewModel.Ip);
            viewModel.Agent       = Net.Browser;
            viewModel.IsAduit     = true;

            _guestBookRepository.Insert(viewModel);

            //缓存用户ip一分钟,用于频繁操作警告
            RedisHelper.StringSet(string.Format(ConstHelper.GuestBook, "IP-" + viewModel.Ip), 1, new TimeSpan(0, 1, 0));

            //留言成功后,给博主的email邮箱发送信息

            _mailHelper.SendByThread(Configs.GetValue("DeveloperEmail"), "有人在你的(、天上有木月)博客发表的留言", UiHelper.FormatEmail(new EmailViewModel
            {
                Title        = "有留言了!!!",
                ToUserName   = "******",
                FromUserName = viewModel.AuName,
                Date         = viewModel.CreatorTime.ToDateTimeString(),
                Content      = viewModel.Text
            }, "EmailTemplate"));

            return(Success("留言成功"));
        }
示例#3
0
        /// <summary>
        /// 根据邮箱发送四位验证码,并将token返回给前端
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>

        public ActionResult GetActiveCode(string email)
        {
            if (email.IsNullOrEmpty())
            {
                return(Error("Email不能为空!!!"));
            }
            if (!Validate.IsEmail(email))
            {
                return(Error("Email格式不正确!!!"));
            }
            int id = Op.CurrentUser.UserId;

            //bool EmailIsValid = _appUserRepository.IQueryable(u => u.Id == Id).Select(r => r.EmailIsValid).FirstOrDefault();

            //if (EmailIsValid)
            //{
            //    return Error("您已经绑定成功了,请不要重复绑定!!!");
            //}

            //为了安全,一个用户IP10分钟内只能请求此接口3次

            string ip    = Net.Ip;
            int    count = _redisHelper.StringGet <int>(ip);

            if (count >= 3)
            {
                return(Error("请求过于频繁,请稍后再试!"));
            }

            count += 1;

            _redisHelper.StringSet(ip, count, new TimeSpan(0, 10, 0));

            var amm = _appUserRepository.IsRepeat(new AppUser
            {
                Id        = id,
                LoginName = Utils.GuId(),
                Email     = email
            });

            if (amm.State.Equals(ResultType.Error.ToString()))
            {
                return(Error("该邮箱已被绑定其他用户绑定!"));
            }

            //缓存1小时
            TimeSpan saveTime = new TimeSpan(1, 0, 0);

            //生成token
            string token    = Utils.GuId();
            string rand4Num = Utils.RndNum(4);

            //redis缓存绑定邮箱随机token作为键,email作为值,随机u为键,当前登录id为值
            _redisHelper.StringSet(token, email, saveTime);
            _redisHelper.StringSet(token + email, rand4Num, saveTime);

            EmailViewModel emailViewModel = new EmailViewModel
            {
                ToUserName = email,
                Code       = rand4Num
            };

            string body = UiHelper.FormatEmail(emailViewModel, "SendCodeTemplate");

            _imailHelper.SendByThread(email, "[、天上有木月博客] 邮箱激活通知", body);

            return(Success("邮箱发送成功,请查收", token));
        }