net 写的app 接口,短信验证码模块的时候,本来验证码是放在session里面的,但是发现第二次会话时,愣是找不到这个验证码。于是查了下资料,不能放在session,更改为放在数据库。要求:1.验证码在一段时间内有效,过期无效2.验证码发送不能太频繁,在一定时间内不能再发送多次。虽然这个app客户端可以限制,但是也会很容易被人抓包。恶意。。。3.数据库的这张表不能无限的增长数据,要有合理的机制。。。

表结构

if OBJECT_ID ('MJC_Validate') is not null
drop table MJC_Validate
GO
create table [dbo].[MJC_Validate](ValidateId int identity(1,1) not null,                  --idUsers_Tel nvarchar(20) not null default'',              --用户手机Validate nvarchar(6) not null default '',               --验证码Send_Time datetime not null default(getdate()),         --发送时间Valid_time datetime not null default(getdate()),        --过期时间Valid_Length int not null default(180),                 --有效期长度(秒)--防止频繁操作 Interval_Length int not null default(60),               --间隔长度(秒)Interval_Time datetime not null default(getdate()),     --间隔期constraint [MJC_Validate_ValidateId] primary key (ValidateId asc)
)

controller 接口

        /// <summary>/// 验证码 注册和手机号登录都需要  获取验证码/// </summary>/// <returns></returns>[HttpGet]public JsonResult check_validate(string tel){try{if (!Helper.IsTelephone(tel)){log.Info("手机号非法");return Json(new { result = 1 }, JsonRequestBehavior.AllowGet);}string code = Helper.GetNumPwd(6);#region 发送验证码ITopClient client = new DefaultTopClient("http://gw.api.taobao.com/router/rest", "23302987", "b74ddcb3540ab637c90cf2ad59c22f1b");AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();req.Extend = "";req.SmsType = "normal";req.SmsFreeSignName = "茗家春茶";//req.SmsParam = "{'code':'963740','product':'alidayu'}";req.SmsParam = "{'code':'" + code + "'}";req.RecNum = tel;req.SmsTemplateCode = "SMS_4915099";//短信模板AlibabaAliqinFcSmsNumSendResponse rsp = client.Execute(req);#endregion//将短信验证码存放到数据库//30 是验证码有效期,20 是验证码频繁期  单位都是秒if (_ivalidate.create_code(tel, code, 30, 20)){string result = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(code, "MD5").ToLower();//验证码已发送到您的手机请耐心等待return Json(new { result = 2 }, JsonRequestBehavior.AllowGet);//return Json(new { result = code }, JsonRequestBehavior.AllowGet);}else{log.Info("操作频繁,稍后再试,");return Json(new { result = 3 }, JsonRequestBehavior.AllowGet);}}catch (Exception e){log.Info("报错", e);return Json(new { result = 500 }, JsonRequestBehavior.AllowGet);}}
        /// <summary>/// 查询验证码/// </summary>/// <param name="code"></param>/// <param name="tel"></param>/// <returns></returns>public JsonResult query_code(string code, string tel){if (_ivalidate.query(tel, code))return Json(new { result = true }, JsonRequestBehavior.AllowGet);elsereturn Json(new { result = false }, JsonRequestBehavior.AllowGet);}

这里就不写interface了

ValidateService

        /// <summary>/// 记录验证码/// </summary>/// <param name="tel"></param>/// <param name="code"></param>/// <returns></returns>public bool create_code(string tel, string code, int ValidLength, int IntervalLength){using (GeContext context = new GeContext(Utility.GetSqlConnectingString())){try{DateTime now = DateTime.Now;// 手机号是否存在var _telModel = context.Ge_Validates.Where(p => p.Users_Tel == tel).OrderByDescending(p => p.ValidateId).ToList();if (_telModel.Count > 1){log.Info("该手机号存在多条验证码");return false;}if (_telModel.Count > 0){var telModel = _telModel.FirstOrDefault();//是否过期if (telModel.Valid_time < now){//过期context.Ge_Validates.Remove(telModel);int re = context.SaveChanges();if (re == 1){if (create(tel, code, ValidLength, IntervalLength)){return true;}else{return false;}}else{log.Info("删除失败");return false;}}else{ //是否频繁if (telModel.Interval_Time < now){//不频繁context.Ge_Validates.Remove(telModel);int re = context.SaveChanges();if (re == 1){if (create(tel, code, ValidLength, IntervalLength)){return true;}else{return false;}}else{log.Info("删除失败");return false;}}else{//频繁log.Info("频繁");return false;}}}else{log.Info("手机号" + tel + "不存在,直接创建");if (create(tel, code, ValidLength, IntervalLength)){return true;}else{return false;}}}catch (Exception e){log.Info(e.Message);return false;}}}public bool create(string tel, string code, int ValidLength, int IntervalLength){using (GeContext context = new GeContext(Utility.GetSqlConnectingString())){try{DateTime now = DateTime.Now;var validateModel = new Ge_ValidateModels{Users_Tel = tel,Validate = code,Send_Time = now,Valid_Length = ValidLength,Valid_time = now.AddSeconds(ValidLength),Interval_Length = IntervalLength,Interval_Time = now.AddSeconds(IntervalLength)};context.Ge_Validates.Add(validateModel);int re = context.SaveChanges();if (re == 1)return true;elsereturn false;}catch (Exception e){log.Info(e.Message);return false;}}}   
        /// <summary>/// 查询验证码/// </summary>/// <param name="tel"></param>/// <param name="code"></param>/// <returns></returns>public bool query(string tel, string code){using (GeContext context = new GeContext(Utility.GetSqlConnectingString())){try{var validateModel = context.Ge_Validates.AsNoTracking().Where(p => p.Validate == code && p.Users_Tel == tel).OrderByDescending(p => p.ValidateId).FirstOrDefault();if (validateModel != null){if (validateModel.Valid_time < DateTime.Now){ //超时log.Info("验证码:" + code + "过期");return false;}else{return true;}}else{log.Info("没有与手机号:" + tel + "相对应的验证码:" + code);return false;}}catch (Exception e){log.Info(e.Message);return false;}}}   

ok 到这步,完成。但是程序跑起来数据库还留有一些数据怎么办。不用担心,每个手机号只能留一条记录,存的是上一次的,新发一条验证码,自然会把现有的数据给覆盖。

PS:我用第三方短信平台是 阿里大鱼 0.045/条

net 服务端接口 存储,发送 app短信验证码相关推荐

  1. 实现app短信验证码功能这样做就很简单!

    现在大多数app短信验证码服务都是由第三方服务商提供的,企业不需要对接运营商就可以让app具备三网发送短信功能,现在app短信验证码使用场景很多,比如说注册.登陆.支付等场景,app短信验证码实现的原 ...

  2. 【微信小程序 - 工作实战分享】1.微信小程序发送手机短信验证码(阿里云)

    发送手机短信验证码 前言 一. 准备工作 二. 配置 三. 实战代码(仅仅是后台代码,前端传入手机号) 总结 前言 在网站和移动应用中利用短信验证码进行信息确认是最常用的验证手段.随着短信验证码的技术 ...

  3. 发送手机短信验证码-后端、前端(验证码倒计时)

    获取手机短信验证码-后端.前端(验证码倒计时) 后端 /*** 发送手机短信验证码** @return str* @throws Exception* @author 王永圣*/@RequestMap ...

  4. 【Gulimall+】第三方服务:对象存储OSS、短信验证、社交登录、支付宝支付

    文章目录 对象存储OSS 基本配置 java实现 短信认证 前端验证码倒计时 整合短信验证码 密码存储 社交登录:微博 基本流程 java实现 支付宝支付 基本配置 java实现 对象存储OSS 一谈 ...

  5. app 短信验证码 突然无法发送

    app已经上线一段时间了,突然短信不好使了... 懵逼了..觉得肯定是第三方的问题..但是还是得查询啊... 打开项目日志....实在是太乱了...根本找不到短信验证码..而且即使有..也看不出来多少 ...

  6. 网站或者APP短信验证码是怎么实现的?

    网站商城.APP一般都需要下发验证码给客户用于注册.改密.登录等等.一般而言,网站.商城.或是APP要实现短信验证码的功能一般是通过接入短信验证码接口来实现的, 以下我会附加短信验证码接口的代码需要学 ...

  7. .NET MVC用Ajax实现发送手机短信验证码

    本次实验基于上次的.net aspx的手机实现短信验证. 本次实验大致过程是: 1.用户在页面输入手机号码进行注册 2.如果手机号已经存在了就会提示已经注册过了 3. 用户点击"发送验证码& ...

  8. 如何发送手机短信验证码

    文章目录 阿里云短信业务实战教程 1.阿里云平台的使用 2.创建用户组及用户并添加权限 3.添加短信签名和短信模板并充值费用 4.开发工具进行代码部分(这里使用IDEA) 阿里云短信业务实战教程 手机 ...

  9. Java调用WebService接口实现发送手机短信验证码功能,java 手机验证码,WebService接口调用...

    近来由于项目需要,需要用到手机短信验证码的功能,其中最主要的是用到了第三方提供的短信平台接口WebService客户端接口,下面我把我在项目中用到的记录一下,以便给大家提供个思路,由于本人的文采有限, ...

最新文章

  1. 天猫权益平台如何10倍的提升数据库查询响应时间
  2. 科技边框_智能手机窄边框喷射点胶机欧力克斯
  3. php 价钱计算,php公式计算
  4. 太阳能板如何串联_光伏板清洁专用的清洁毛刷
  5. java super.start,java – 在字节码中确定哪里是super()方法调用所有构造函数必须在JVM上执行...
  6. 题解 AT5258 【[ABC156A] Beginner】
  7. 2853: 小A的游戏昵称(郑轻oj)
  8. html 文字中不换行怎么写,HTML让字体在一行内显示不换行
  9. 哪一个瞬间,点燃了你的离职决心?
  10. LINUX加载静态库so,取得函数地址并调用
  11. curl命令多行执行
  12. 屏幕取色器,windows
  13. qq邮箱imtp收件服务器,邮件客户端和手机设置QQ邮箱IMAP服务
  14. 人生无常,心安便是归处
  15. 【Python-Numpy】numpy.random.binomial()的解析与使用
  16. DOTA高考全国卷A卷
  17. 有人负责,才有质量:写给在集市中迷失的一代
  18. 从产物追溯研发合成路线?自建化合物数据库溯源不再成难题
  19. html转图片并解决模糊问题
  20. carbondata1.5.1编译

热门文章

  1. Angular完整项目开发13 - 使用图表(ng2-charts+chart.js)
  2. 什么是软件生命周期模型,比较几个模型的优缺点
  3. iOS10.1.1越狱插件推荐——DarkAppleStore
  4. 初学平面设计如何制作出有创意的作品
  5. ADC中的可编程增益放大器PGA
  6. steam()的应用
  7. AtmelStudio 7 ASF库学习笔记一:新建工程、配置时钟、下载程序和查看官方例程
  8. 吉林省二级计算机vb c语言 access vf 考哪个有用,计算机二级考试选择
  9. 源码环境下跟进MO通话流程(涉及到其中每一个方法的跳转)
  10. Webpack打包和优化