asp.net mvc 接入阿里大于短信验证码发送

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

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

2步:创建应用

3步:配置短信签名

4步:配置短信模板

5步:前端

trclass="margin-top"

tdclass="padding-top text-center"手机号/td

tdinputtype="text"class="inputs"id="Phone"name="Phone"/td

tdinputtype="button"value="获取验证码"id="sms"onclick="sendemail()"/td

/tr

tr

tdclass="padding-top text-center"验证码/td

tdinputtype="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"));//开始倒计时

}

})

//发送验证码

functionsendCode(obj) {

varphoneNumber = $("#Phone").val();

varresu< = isPhoneNum(phoneNumber);

if(resu< ) {

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

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

if(data == "ok") {

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

} else{

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

}

});

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

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

}

}

//开始倒计时

varcountdown;

functionsettime(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毫秒执行一次

}

//校验手机号是否合法

functionisPhoneNum(phoneNumber) {

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

if(!myreg.test(phoneNumber)) {

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

returnfalse;

} else{

returntrue;

}

}

//发送验证码时添加cookie

functionaddCookie(name, value, expiresHours) {

varcookieString = name + "="+ escape(value);

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

if(expiresHours 0) {

vardate = newDate();

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

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

}

document.cookie = cookieString;

}

//修改cookie的值

functioneditCookie(name, value, expiresHours) {

varcookieString = name + "="+ escape(value);

if(expiresHours 0) {

vardate = newDate();

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

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

}

document.cookie = cookieString;

}

//根据名字获取cookie的值

functiongetCookieValue(name) {

varstrCookie = document.cookie;

vararrCookie = strCookie.split("; ");

for(vari = 0; i arrCookie.len >h; i++) {

vararr = arrCookie[i].split("=");

if(arr[0] == name) {

returnunescape(arr[1]);

break;

} else{

return"";

break;

}

}

}

7步:后台控制器处理

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

publicActionResu< ValidateCode()

{

stringCode = GetRandomString(6);

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

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

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

ITopClientclient = newDefau< TopClient(url, appkey, secret);

AlibabaAliqinFcSmsNumSendRequestreq = newAlibabaAliqinFcSmsNumSendRequest();

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

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

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

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

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

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

AlibabaAliqinFcSmsNumSendResponsersp = client.Execute(req);

if(rsp.IsError == false)

{

Console.WriteLine(rsp.Body);

//return Content(rsp.Body);

}

//将验证码设置缓存

varCodeInfo = (Object)Code;

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

returnContent("ok");

}

#region生成6位验证码

publicstringGetRandomString(intiLen >h)

{

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

StringBuildersb = newStringBuilder();

Randomr = newRandom();

intrange = buffer.Len >h;

for(inti = 0; i iLen >h; i++)

{

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

}

returnsb.ToString();

}

#endregion

8:缓存处理

publicclassCacheOpt

{

///summary

///设置缓存

////summary

///param name="CacheKey"/param

///param name="objObject"/param

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

publicstaticvoidSetCache(stringCacheKey, objectobjObject, longSeconds)

{

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

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

}

///summary

///获取数据缓存

////summary

///param name="CacheKey"/param

publicstaticobjectGetCache(stringCacheKey)

{

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

returnobjCache[CacheKey];

}

}

publicclassCacheOpt

{

///summary

///设置缓存

////summary

///param name="CacheKey"/param

///param name="objObject"/param

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

publicstaticvoidSetCache(stringCacheKey, objectobjObject, longSeconds)

{

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

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

}

///summary

///获取数据缓存

////summary

///param name="CacheKey"/param

publicstaticobjectGetCache(stringCacheKey)

{

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

returnobjCache[CacheKey];

}

}

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

asp.net mvc 接入阿里大于 短信验证码发送

原文地址:http://www.cnblogs.com/zouke1220/p/7267984.html


最新回复(0)
/jishup88yiY6dZNTL5tpdBHZZZWZP86giKqvN_2Fh_2BF5A_3D_3D4719289
8 简首页