zoukankan      html  css  js  c++  java
  • 手机短信验证码发送频繁请求的限制

    概要

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

    内容

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

    本章准备用缓存来做请求时间的限制,如下我们的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);
                }
    
  • 相关阅读:
    服务器监控
    hadoop
    tomcat7 配置
    tomcat
    列表加载
    自适应网页设计(Responsive Web Design)
    Android Native 代码NDK开发学习笔记
    NDK开发历程(一):android native code的调试方法
    Android原生(Native)C开发之一:环境搭建篇
    ajaxfileupload 传参数
  • 原文地址:https://www.cnblogs.com/bestfriends/p/sms-request-limit.html
Copyright © 2011-2022 走看看