本文来自于stoneniqiu的文章,原文地址 http://www.cnblogs.com/stoneniqiu/p/6234002.html

1.注册一个应用

得到AppKey 和 App Secret     应用管理-->应用列表

2.设置签名

配置管理-->验证码

签名是出现短信内容最前面的字段,比如【xx科技】xxxx,

这个需要审核。显示是2小时内。

3.设置模板

模板就是用来组织短信内容的部分

4. 应用测试

完成上面3步之后,我们就可以测试下,在应用管理--应用测试

https://www.alidayu.com/center/application/test

测试选择好模板,输入签名、电话号码就可以发送了。

5.代码调试

需要先下载个sdk,.net是TopSDK.dll。如果是https,对应的地址是:https://eco.taobao.com/router/rest

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Niqiu.Core.Helpers;
using Top.Api;
using Top.Api.Request;
using Top.Api.Response; namespace Portal.MVC.Controllers
{
public class AliMessageController : Controller
{
//
// GET: /AliMessage/ public static string url = "http://gw.api.taobao.com/router/rest";
public static string appkey = "--583689";
public static string secret = "0---6861cb74da5ac98c02c1172---0";
public ActionResult Index()
{
var res = SendRandomCodeToMobile("1xxxxxxxxxx", "stoneniqiu");
return res;
} public JsonResult SendRandomCodeToMobile(string phone,string username)
{
ITopClient client = new DefaultTopClient(url, appkey, secret);
AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
req.Extend = "";
req.SmsType = "normal";
req.SmsFreeSignName = "好油菜";
var randomCode = GetID();
//req.SmsParam = "{name:'stone',number:'3345'}";
req.SmsParam = "{name:'" + username + "',number:'" + randomCode + "'}";
req.RecNum = phone;
req.SmsTemplateCode = "SMS_36290127";
AlibabaAliqinFcSmsNumSendResponse rsp = client.Execute(req);
Console.WriteLine(rsp.Body);
//存储 结果,发送时间,随机数和手机号
if (rsp.IsError)
{
Logger.Debug(rsp.ErrCode + " " + rsp.ErrMsg);
}
return Json(new { success = !rsp.IsError, message = rsp.ErrMsg, code = rsp.ErrCode },JsonRequestBehavior.AllowGet);
} private int GetID()
{
Random rd = new Random();
int num = rd.Next(1000, 9999);
return num; } }
}

每个号码有流量限制:

测试的时候一小时超过7条就收不到了。发送短信的逻辑就是这么多了,如果要验证用户收到的验证码是否一致 这个就简单了,存储每次发送的手机号和对应的验证码,验证的时候对比下就行了。然后因为该服务是一分钟一条的,所以需要限制下两次获取验证码的间隔是1分钟。这些逻辑都蛮简单的。每个账号有200条免费的可以玩。

6.用例

短信验证可以用于注册或者忘记密码:

html:

<div class="page" id="register">
<div class="logbg" >
<div class="logtitle">注册好油菜</div>
<div class="regnum">
<input type="tel" id="regtel" placeholder="输入你的手机号码">
<input type="button" id="getvcode" value="获取验证码" readonly>
</div>
<div class="regyzm">
<p>输入你手机收到的4位验证码</p>
<div class="yzmbox">
<input type="number">
<input type="number">
<input type="number">
<input type="number" class="marg0">
</div>
</div>
<div class="inputbox">
<input type="text" id="name" placeholder="输入昵称">
<input type="text" id="regpwd" placeholder="输入6-12位数字和字母密码">
<input type="text" id="crep" placeholder="再次输入上面的密码">
</div>
<input type="submit" id="registerbt" class="logbtn logbtn1" value="确认注册">
</div>
<div class="btmbg"></div> </div>

js:

 //获取验证码
$(document).on("click", "#getvcode", function() {
//先验证正确的手机号
var tel = $("#regtel").val();
var btn = $(this);
var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
if (!myreg.test(tel)) {
$.alert("请输入正确的手机号码");
return false;
}
//触发后台发送
//post
$.post("/AliMessage/SendRandomCodeToMobile", { phone: tel }, function(data) {
if (data.success) { $.toast("验证码发送成功,15分钟内有效哦!");
//开始倒计时
var count = 60;
//disabled
btn.css("font-size", "1rem").val(count).attr("disabled", "disabled");
var st = setInterval(function () {
count--;
btn.val(count);
if (count <= 1) {
//恢复点击
btn.css("font-size", ".5rem").val("获取验证码").removeAttr("disabled");
clearInterval(st);
}
}, 1000); }
});
});
//验证码输入
$(document).on("keyup", ".yzmbox input", function() {
$(this).next().focus();
});
 var pathname = location.href;
var repath = "/";
if (pathname.indexOf("returnUrl") > -1) {
repath = pathname.split('returnUrl=')[1];
}
console.log(pathname,repath);
$(document).on("click", "#registerbt", function () {
var mobile = $("#regtel").val();
var name = $("#name").val();
var pwd = $("#regpwd").val();
var cpwd = $("#crep").val();
var code = $(".yzmbox input").eq(0).val() + $(".yzmbox input").eq(1).val() + $(".yzmbox input").eq(2).val() + $(".yzmbox input").eq(3).val();
if (!mobile) {
$.toast("请输入手机号码");
return;
}
if (!name|| !pwd) {
$.toast("请输入用户名和密码");
return;
}
if (pwd != cpwd) {
$.toast("两次输入密码不一致");
return;
}
console.log("code",code);
$.post('@Url.Action("RegisterJson")', { mobile: mobile, name: name, password: pwd, compassword: cpwd ,code:code}, function (res) {
if (res === 1) {
$.toast("注册成功");
setTimeout(function() {
location.href = repath;
}, 1000); } else {
$.toast(res);
}
});
})

serivces:

public class PhoneCodeSerice : IPhoneCodeSerice
{
private readonly IRepository<PhoneCode> _pRepository;
public PhoneCodeSerice(IRepository<PhoneCode> repository)
{
_pRepository = repository;
} public void Insert(PhoneCode model)
{
_pRepository.Insert(model);
} public bool Valid(string code, string phone)
{
//多长时间内有效 15分钟呢
var endTime = DateTime.Now.AddMinutes(-15);
return _pRepository.Table.Any(n => n.CreateTime >= endTime && n.Mobile == phone && n.Code == code);
}
}
 

忘记密码就大同小异了

        public ActionResult ForgetPwdJson(string mobile,string code,string password)
{
var codevalid = _phoneCodeSerice.Valid(code, mobile);
if (!codevalid) return Json("验证码错误", JsonRequestBehavior.AllowGet); if (string.IsNullOrEmpty(password) || password.Length < 6)
{
return Json("密码不能", JsonRequestBehavior.AllowGet);
} var user = _service.GetUserByMobile(mobile);
_accountService.ChangePassword(user.Id, mobile);
AuthenticationService.SignIn(user, true); return Json(1);
}
 $(document).on("click", "#forgetbt", function () {
var mobile = $("#regtel").val();
var pwd = $("#regpwd").val();
var cpwd = $("#crep").val();
var code = $(".yzmbox input").eq(0).val() + $(".yzmbox input").eq(1).val() + $(".yzmbox input").eq(2).val() + $(".yzmbox input").eq(3).val();
if (!mobile) {
$.toast("请输入手机号码");
return;
}
if (pwd != cpwd) {
$.toast("两次输入密码不一致");
return;
}
console.log("code", code);
$.post('@Url.Action("ForgetPwdJson")', { mobile: mobile, code: code, password: pwd }, function (res) {
if (res === 1) {
$.toast("修改成功");
setTimeout(function () {
location.href = '@Url.Action("Index","Home")';
}, 1000);
} else {
$.toast(res);
}
});
})

ASP.NET MVC 手机短信验证的更多相关文章

  1. 如何实现php手机短信验证功能

    http://www.qdexun.cn/jsp/news/shownews.do?method=GetqtnewsdetailAction&id=1677 下载php源代码 现在网站在建设网 ...

  2. 完整的Android手机短信验证源码

    短信验证功能我分两个模块来说,短信验证码的后台和代码实现短信验证码的功能. 一.短信验证码的后台      1.注册Mob账号:http://www.mob.com/#/login 2.注册成功之后, ...

  3. 利用twilio进行手机短信验证

    首先要注册 twilio 账号但是由于twilio人机验证用的是Goole所有注册需要FQ 完成后去免费获取15元使用 然后 pip install twilio 注册完成后会在个人首页显示你的免费金 ...

  4. 第一次发博,发个简单的Java程序发送手机短信验证

    最近在准备一个项目,想的登录时候用手机验证,就通过上网查阅了一下手机验证的实现方法,原来超级简单,下面将一步一步介绍. 1.去中国网建注册一个账号密码,首次注册送五条免费短信和3条免费彩信.具体的网址 ...

  5. NodeJS 实现手机短信验证 模块阿里大于

    1,NodeJS 安装阿里大于模块 切换到项目目录使用npm 安装阿里于模块 npm i node-alidayu --save 2,aliyu官网使用淘宝账户登录 登录阿里大于 https://do ...

  6. js手机短信验证

    贴代码之前,我们先讲一下这里我们用到的技术主要有1个.setInterval(),这个方法可以实现倒计时的效果. css: .weui_btn_disabled.weui_btn_default { ...

  7. 融云发送手机短信验证短信(.net版本)

    首先本次需求是在MVC5中进行的,通过收费的融云服务来验证手机号码,而且本次的项目也是前后台分离,所以添加了WEBAPI2,那么先添加WEBAPI的接口 using System; using Sys ...

  8. 【转】用JS完成手机短信验证按键点击事件

    原地址:https://gitee.com/RainVanilla/codes/i7jske4wdogvnb0apmfx571 试了一下,效果还可以,留着备用! <!DOCTYPE html&g ...

  9. ASP.NET MVC+Bootstrap 实现短信验证

    短信验证大家都已经非常熟悉了,基本上每天都在接触手机短信的验证码,比方某宝,某东购物.站点注冊,网上银行等等,都要验证我们的手机号码真实性.这样做有什么优点呢. 曾经咱们在做站点的时候.为了提高用户注 ...

随机推荐

  1. Html.DropDownListFor() 二级联动 ($.getJSON)

    Control: public ActionResult GetPositionName(int parentid) //发布新职位页面中的根据职位类别,获取职位名称 { List<Catego ...

  2. Python模块:Random(未完待续)

    本文基于Python 3.6.5的官文random编写. random模块简介 random为各种数学分布算法(distributions)实现了伪随机数生成器. 对于整数,是从一个范围中均匀选择(u ...

  3. Failed to load class "org.slf4j.impl.StaticLoggerBinder"

    调试程序出现如下错误: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".SLF4J: Default ...

  4. 【小程序开发】购物车加减几件demo

    <!-- 主容器 --> <view class="stepper"> <!-- 减号 --> <text class="{{m ...

  5. 分析new delete 的本质

    在程序设计中,数据可能会存在不同的内存空间,如函数栈 堆   全局变量区  ,今天我们来分析一下C++中堆分配方式和C语言的堆分配方式异同,从而更好的理解new  delete本质 C语言使用mall ...

  6. HDU 1054 Strategic Game(最小路径覆盖)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1054 题目大意:给你一棵树,选取树上最少的节点使得可以覆盖整棵树. 解题思路: 首先树肯定是二分图,因 ...

  7. Python学习笔记:出生日期转化为年龄

    在数据挖掘项目中,有时候个体的出生日期包含信息量过大,不适合作为一个有效数据进入模型算法训练,因此有必要把出生日期转化为年龄age,age是一个很好的特征工程指示变量. import pandas a ...

  8. 使用celery时要注意的任务调用形式

    因为之前,一直用django和celery紧密集成,不分家. 所以使用时参考了网上的配置之后,没有变更过. 最近,和洪军想用k8s的pod重新规划系统构架时,这个问题才又浮了出来. 只是我们的task ...

  9. oracle创建job和删除job

    https://blog.csdn.net/u010001043/article/details/56479774

  10. markdown转换为html

    想要自己实现markdown编辑器,欲使用markdown-it作为编辑器,有着比较多的插件,可以实现代码高亮以及对数学公式转换等功能. // Activate/deactivate rules, w ...