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


    概要

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

    内容

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

    本章准备用缓存来做请求时间的限制,如下我们的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);
                }
    
  • 相关阅读:
    由 container 一词所想到的
    突然间,firebug中不显示用console.log打印的信息了
    学习计划表-快照-2017.2.16
    学习编程让我成功减肥!
    什么是编程?
    计算两个事件之间的时间差
    使用substring和split方法从字符串中抽取一组清单
    js中十进制数转换为16进制
    Definition of success-成功的定义
    如何让老式浏览器支持html5新增的语义元素
  • 原文地址:https://www.cnblogs.com/bestfriends/p/sms-request-limit.html
Copyright © 2020-2023  润新知