项目前端页面实例

第1步:登录阿里大于控制台

https://www.alidayu.com/center/user/account?spm=0.0.0.0.P1K1jG

第2步:创建应用

第3步:配置短信签名

第4步:配置短信模板

第5步:前端

<tr class="margin-top">

<td class="padding-top text-center">手机号</td>

<td><input type="text" class="inputs" id="Phone" name="Phone"> </td>

<td><input type="button" value="获取验证码" id="sms" onclick="sendemail()"></td>

</tr>

<tr>

<td class="padding-top text-center">验证码</td>

<td><input type="text" class="inputs" id="Code" name="Code"></td>

</tr>

第6步:js处理

$(function () {

$("#sms").click(function () {

sendCode($("#sms"));

});

v = getCookieValue("secondsremained");//获取cookie值

if (v > 0) {

settime($("#sms"));//开始倒计时

}

})

//发送验证码

function sendCode(obj) {

var phoneNumber = $("#Phone").val();

var result = isPhoneNum(phoneNumber);

if (result) {

//将手机利用ajax提交到后台的发短信接口

$.post("/College/Code", { Phone: phoneNumber }, function (data) {

if (data == "ok") {

alert("验证码发送成功!");

} else {

alert("验证码发送失败,请重新发送!");

}

});

addCookie("secondsremained", 60, 60);//添加cookie记录,有效时间60s

settime(obj);   //开始倒计时

}

}

//开始倒计时

var countdown;

function settime(obj) {

countdown = getCookieValue("secondsremained");

if (countdown == 0) {

obj.removeAttr("disabled");

obj.val("获取验证码");

return;

} else {

obj.attr("disabled", true);

obj.val("重新发送(" + countdown + ")");

countdown--;

editCookie("secondsremained", countdown, countdown + 1);

}

setTimeout(function () { settime(obj) }, 1000) //每1000毫秒执行一次

}

//校验手机号是否合法

function isPhoneNum(phoneNumber) {

var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;

if (!myreg.test(phoneNumber)) {

alert('请输入有效的手机号码!');

return false;

} else {

return true;

}

}

//发送验证码时添加cookie

function addCookie(name, value, expiresHours) {

var cookieString = name + "=" + escape(value);

//判断是否设置过期时间,0代表关闭浏览器时失效

if (expiresHours > 0) {

var date = new Date();

date.setTime(date.getTime() + expiresHours * 1000);

cookieString = cookieString + ";expires=" + date.toUTCString();

}

document.cookie = cookieString;

}

//修改cookie的值

function editCookie(name, value, expiresHours) {

var cookieString = name + "=" + escape(value);

if (expiresHours > 0) {

var date = new Date();

date.setTime(date.getTime() + expiresHours * 1000); //单位是毫秒

cookieString = cookieString + ";expires=" + date.toGMTString();

}

document.cookie = cookieString;

}

//根据名字获取cookie的值

function getCookieValue(name) {

var strCookie = document.cookie;

var arrCookie = strCookie.split("; ");

for (var i = 0; i < arrCookie.length; i++) {

var arr = arrCookie[i].split("=");

if (arr[0] == name) {

return unescape(arr[1]);

break;

} else {

return "";

break;

}

}

}

第7步:后台控制器处理

#region 商学院报名发送验证码

public ActionResult ValidateCode()

{

string Code = GetRandomString(6);

string url = "https://eco.taobao.com/router/rest";

string appkey = "****";  //此处填写你自己的

string secret = "****";   //此处填写你自己的

ITopClient client = new DefaultTopClient(url, appkey, secret);

AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();

req.Extend = "";              //可空,返回状态

req.SmsType = "normal";             //不可更改

req.SmsFreeSignName = "个人小站";       //申请的短信签名,不可填写与申请的不一

req.SmsParam = "{VCode:'" + Code + "'}";  //模板内参数必填

req.RecNum = Request["Phone"];         //接收者手机号码

req.SmsTemplateCode = "SMS_74235011";    //短信模板的编号,不可出错

AlibabaAliqinFcSmsNumSendResponse rsp = client.Execute(req);

if (rsp.IsError == false)

{

Console.WriteLine(rsp.Body);

//return Content(rsp.Body);

}

//将验证码设置缓存

var CodeInfo = (Object)Code;

CacheOpt.SetCache("Code", CodeInfo, Convert.ToInt32(60));

return Content("ok");

}

#region 生成6位验证码

public string GetRandomString(int iLength)

{

string buffer = "0123456789";    // 随机字符中也可以为汉字(任何)

StringBuilder sb = new StringBuilder();

Random r = new Random();

int range = buffer.Length;

for (int i = 0; i < iLength; i++)

{

sb.Append(buffer.Substring(r.Next(range), 1));

}

return sb.ToString();

}

#endregion

第8步:缓存处理

public class CacheOpt

{

/// <summary>

/// 设置缓存

/// </summary>

/// <param name="CacheKey"></param>

/// <param name="objObject"></param>

/// <param name="Seconds">超过多少秒后过期</param>

public static void SetCache(string CacheKey, object objObject, long Seconds)

{

System.Web.Caching.Cache objCache = HttpRuntime.Cache;

objCache.Insert(CacheKey, objObject, null, System.DateTime.Now.AddSeconds(Seconds), TimeSpan.Zero);

}

/// <summary>

/// 获取数据缓存

/// </summary>

/// <param name="CacheKey">键</param>

public static object GetCache(string CacheKey)

{

System.Web.Caching.Cache objCache = HttpRuntime.Cache;

return objCache[CacheKey];

}

}

public class CacheOpt

{

/// <summary>

/// 设置缓存

/// </summary>

/// <param name="CacheKey"></param>

/// <param name="objObject"></param>

/// <param name="Seconds">超过多少秒后过期</param>

public static void SetCache(string CacheKey, object objObject, long Seconds)

{

System.Web.Caching.Cache objCache = HttpRuntime.Cache;

objCache.Insert(CacheKey, objObject, null, System.DateTime.Now.AddSeconds(Seconds), TimeSpan.Zero);

}

/// <summary>

/// 获取数据缓存

/// </summary>

/// <param name="CacheKey">键</param>

public static object GetCache(string CacheKey)

{

System.Web.Caching.Cache objCache = HttpRuntime.Cache;

return objCache[CacheKey];

}

}

注:完整版项目地址:http://www.gmkcn.com/

asp.net mvc 接入阿里大于 短信验证码发送的更多相关文章

  1. asp.net mvc 接入美圣短信 验证码发送

    第1步:登录美圣短信控制台 http://www.rcscloud.cn/hy/HY_ZH/login 账号:******* 密码:******* http://www.rcscloud.cn/com ...

  2. ASP版_阿里大于短信API Demo

    阿里大于申请地址:http://www.alidayu.com 阿里大于短信发送Demo: ******index.asp************* <%@LANGUAGE="VBSC ...

  3. Aps.Net Core3.1 WebApi发送阿里云短信验证码

    1.前言 转眼又要过了一年了 好久没写博客了,人不学就要落后,今天有时间把以前弄的发送阿里云短信验证码登录记录一下. 2.准备条件 1)去阿里云官网注册一个账号.有账号直接登录就行,以前新人好像有免费 ...

  4. php实现的IMEI限制的短信验证码发送类

    php实现的IMEI限制的短信验证码发送类 <?php class Api_Sms{ const EXPIRE_SEC = 1800; // 过期时间间隔 const RESEND_SEC = ...

  5. PHP实现对短信验证码发送次数的限制(防机刷验证码)

    PHP实现对短信验证码发送限制(防止机刷验证码) 对用户获取短信验证码的手机号.ip.和浏览器(使用唯一标识)进行限制.本文介绍的方法是对用户每天只能通过同一浏览器或同一ip地址获取验证码10次或者同 ...

  6. Thinkphp5使用阿里大于短信验证

    现在各种平台登录验证很多时候会使用短信验证,快捷安全,有很多平台提供短信验证服务,相比较而言阿里大于价格比较便宜,快捷,所以在在千锋日常的php教学中多以此为例来说明短信验证的使用.下面我们在tp5中 ...

  7. thinkphp5阿里大于短信接口

    function autumn_sendsms($tel,$stype){ $pd_go=true; if($tel==''){ $msg='手机号不能为空'; $pd_go=false; } if( ...

  8. ThinkPHP3.2.3框架下接入阿里云短信服务接口实现:注册登录

    首先介绍下短信注册登录流程: 注册页面点击获取手机号验证码按钮,用jquery的click事件POST或GET方法把手机号发送到后台控制器: 后台控制器创建函数,收到手机号后生成随机码,例如:6位的随 ...

  9. TP3.2.3 接入阿里sms 短信接口

    阿里云短信接口 配置文件 config.php //阿里大鱼 'Ali_SMS' =>array( 'sms_temp' =>'短信模板', 'sms_sign' =>'签名', ' ...

随机推荐

  1. 关于abp中使用的sweetalert对话框组件的confirm确认对话框中的一个坑

    今天修改了一个功能,限制删除用户,在删除的时候不满足条件的时候提示用户原因,使用的sweet alert组件. abp框架前端集成了sweet alert 对http请求的error做了全局处理,我在 ...

  2. Memcached的基础梳理

    1 .Memcached 概念 官方解释如下: What is Memcached? Free & open source, high-performance, distributed mem ...

  3. 如何写出面试官欣赏的Java单例

    单例模式是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例的特殊类.通过单例模式可以保证系统中一个类只有一个实例. 今天我们不谈单例模式的用途,只说一说如果在面试的时候面试官让你敲一段代码 ...

  4. 流畅python学习笔记:第十五章:上下文管理器

    在开始本章之前,我们首先来谈谈try-excep..final模块.在Python中,进行异常保护的最多就是用try..except..final.首先来看下下面的代码.进行一个简单的除法运算.为了防 ...

  5. 一步一步学习Vue(十一)

    本篇继续学习vuex,还是以实例为主:我们以一步一步学Vue(四)中讲述的例子为基础,对其改造,基于vuex重构一遍,这是原始的代码: todolist.js ; (function () { var ...

  6. Redux源码分析之combineReducers

    Redux源码分析之基本概念 Redux源码分析之createStore Redux源码分析之bindActionCreators Redux源码分析之combineReducers Redux源码分 ...

  7. LoadRunner性能测试-loadrunner工具破解

    Loadrunner11破解 破解工具下载:http://pan.baidu.com/disk/home?errno=0&errmsg=Auth%20Login%20Sucess&&a ...

  8. Classy(排序)

    Description In his memoir So, Anyway. . ., comedian John Cleese writes of the class di erence betwee ...

  9. Grass Cownoisseur[Usaco2015 Jan]

    题目描述 In an effort to better manage the grazing patterns of his cows, Farmer John has installed one-w ...

  10. Java面试容易容易出现的一些考点

    考点内容是我个人的一点看法,不代表一定是这些,后面会慢慢继续补充 请写出final.finally.finalize的区别 1.final和finally都是关键字.而finalize是一个方法,是属 ...