ASP.NET MVC 手机短信验证
本文来自于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 手机短信验证的更多相关文章
- 如何实现php手机短信验证功能
http://www.qdexun.cn/jsp/news/shownews.do?method=GetqtnewsdetailAction&id=1677 下载php源代码 现在网站在建设网 ...
- 完整的Android手机短信验证源码
短信验证功能我分两个模块来说,短信验证码的后台和代码实现短信验证码的功能. 一.短信验证码的后台 1.注册Mob账号:http://www.mob.com/#/login 2.注册成功之后, ...
- 利用twilio进行手机短信验证
首先要注册 twilio 账号但是由于twilio人机验证用的是Goole所有注册需要FQ 完成后去免费获取15元使用 然后 pip install twilio 注册完成后会在个人首页显示你的免费金 ...
- 第一次发博,发个简单的Java程序发送手机短信验证
最近在准备一个项目,想的登录时候用手机验证,就通过上网查阅了一下手机验证的实现方法,原来超级简单,下面将一步一步介绍. 1.去中国网建注册一个账号密码,首次注册送五条免费短信和3条免费彩信.具体的网址 ...
- NodeJS 实现手机短信验证 模块阿里大于
1,NodeJS 安装阿里大于模块 切换到项目目录使用npm 安装阿里于模块 npm i node-alidayu --save 2,aliyu官网使用淘宝账户登录 登录阿里大于 https://do ...
- js手机短信验证
贴代码之前,我们先讲一下这里我们用到的技术主要有1个.setInterval(),这个方法可以实现倒计时的效果. css: .weui_btn_disabled.weui_btn_default { ...
- 融云发送手机短信验证短信(.net版本)
首先本次需求是在MVC5中进行的,通过收费的融云服务来验证手机号码,而且本次的项目也是前后台分离,所以添加了WEBAPI2,那么先添加WEBAPI的接口 using System; using Sys ...
- 【转】用JS完成手机短信验证按键点击事件
原地址:https://gitee.com/RainVanilla/codes/i7jske4wdogvnb0apmfx571 试了一下,效果还可以,留着备用! <!DOCTYPE html&g ...
- ASP.NET MVC+Bootstrap 实现短信验证
短信验证大家都已经非常熟悉了,基本上每天都在接触手机短信的验证码,比方某宝,某东购物.站点注冊,网上银行等等,都要验证我们的手机号码真实性.这样做有什么优点呢. 曾经咱们在做站点的时候.为了提高用户注 ...
随机推荐
- python并发爬虫利器tomorrow(一)
tomorrow是我最近在用的一个爬虫利器,该模块属于第三方的一个模块,使用起来非常的方便,只需要用其中的threads方法作为装饰器去修饰一个普通的函数,既可以达到并发的效果,本篇将用实例来展示to ...
- maven profile 优先级
maven profile是有优先级别 也就是说在setting.xml的profile优先级比pom中同名的profile高. 可以使用 mvn help:active-profiles 这个命令是 ...
- mysql命令gruop by报错this is incompatible with sql_mode=only_full_group_by
在mysql 工具 搜索或者插入数据时报下面错误: ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause ...
- JAVA中分为基本数据类型和引用数据类型区别
一.基本数据类型: byte:Java中最小的数据类型,在内存中占8位(bit),即1个字节,取值范围-128~127,默认值0 short:短整型,在内存中占16位,即2个字节,取值范围-32768 ...
- python网络编程-socket“粘包”(小数据发送问题)
一:什么是粘包 “粘包”, 即服务器端你调用时send 2次,但你send调用时,数据其实并没有立刻被发送给客户端,而是放到了系统的socket发送缓冲区里,等缓冲区满了.或者数据等待超时了,数据才会 ...
- Nginx - keepliave 相关知识点
目录 - 1. 前言- 2. keepalive 介绍- 3. Nginx 与 keepalive 的关系 - 3.1 Nginx - keepalive_timeout - 3.2 Ng ...
- phpStudy配置https
phpStudy配置https 1.打开vhosts-conf配置文件 2.在配置文件中增加如下内容 server { listen 443; server_name tam.gogugong.com ...
- 从源码分析StringUtils包
今天用到StringUtils.join方法,闲来无聊,看了下源码 当然不可能自己分析,你傻啊,在这里推荐一个别人分析的; http://blog.csdn.net/baidu_31071595/ar ...
- 20165203 实验二 Java面向对象程序设计
20165203 实验二 Java面向对象程序设计 一.面向对象程序设计1--单元测试和TDD 1.实验要求 参考 (http://www.cnblogs.com/rocedu/p/6371315.h ...
- TreeMap和TreeSet在排序时如何比较元素?Collections工具类中的sort()方法如何比较元素?
TreeSet要求存放的对象所属的类必须实现Comparable接口,该接口提供了比较元素的compareTo()方法,当插入元素时会回调该方法比较元素的大小.TreeMap要求存放的键值对映射的键必 ...