概要
手機短信發送頻繁請求的限制
內容
手機短信發送,為了避免被人惡意的頻繁請求我們短信,所以一般我們都在短信發送的時候我們都要做一個短信請求發送的限制
本章准備用緩存來做請求時間的限制,如下我們的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);
}