手机短信验证码发送频繁请求的限制

概要

手机短信发送频繁请求的限制

内容

手机短信发送,为了避免被人恶意的频繁请求我们短信,所以一般我们都在短信发送的时候我们都要做一个短信请求发送的限制

本章准备用缓存来做请求时间的限制,如下我们的html代码:

    <div class="container">
        <input type="text" name="verifyCode" id="verifyCode" />
        <input type="button" id="verifyCodeSend" value="获取验证码" />
    </div>  

接着,如下我们的手机短信请求处理接口:

try
            {
                string phone = Request["phone"];
                if (String.IsNullOrEmpty(phone))
                    return Json(new { errcode = 1001, errmsg = "手机号不能为空" }, JsonRequestBehavior.AllowGet);
                Random random = new Random();
                string smsCode = random.Next(1000, 9999).ToString();
                if (CacheHelper.GetCache("Sms_" + phone) != null)
                {
                    if (CacheHelper.GetCache("Interval_" + phone) != null)
                    {
                        //CacheHelper.SetCache("Sms_" + phone, null);
                        return Json(new { errcode = 1001, errmsg = "验证码请求过于频繁,请稍后再试" }, JsonRequestBehavior.AllowGet);
                    }
                }
                else
                {
                    CacheHelper.SetCache("Sms_" + phone, smsCode, DateTime.Now.AddMinutes(5), Cache.NoSlidingExpiration);  //缓存短信验证码
                    CacheHelper.SetCache("Interval_" + phone, smsCode, DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration);  //缓存请求验证码的请求间隔时间
                }
                WebRequestHelper.GetHtml(String.Format(SMS_REQUEST_WEBSITE, smsCode, MESSGAES_CONTENT));
                return Json(new { errcode = 1, errmsg = "发送成功" }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(new { errcode = 1110, errmsg = ex.Message }, JsonRequestBehavior.AllowGet);
            }
posted @ 2016-03-08 18:09  最佳损友。  阅读(11798)  评论(0编辑  收藏  举报