有的网站需要用户的真实信息,例如,通过接收手机验证码然后注册,有的通过电子邮件验证进行注册等等,一般流程如下:
1、需要发送手机短信的接口,这个接口可以向第三方购买;
2、界面设计有输入手机号码,与发送手机验证码的按钮;
3、考虑时效性,需要用Cookie记录时长,例如:30秒,超过30秒验证码则无效;
4、手机号码保存到数据库;
代码如下:
JS代码:
<script src="js/jquery-2.1.0.min.js" type="text/javascript"></script>
<script language="javascript">
var i=30;
function FunCanUse()
{
var t;
if(i==30)
{
var vMobile=$("#txtMoblie").val();
$.ajax({
type: "GET",
dataType: "html",
url: "/AjaxFile/MobileRandCode.ashx",
data: "U=" + encodeURIComponent(vMobile) + "&action=delete&T=" + new Date(),
success: function(result) {
alert(vMobile);
}
})
}
document.getElementById('btnSendCheckCode').disabled=true;//页面加载后禁用按钮
i=i-1;
document.getElementById('btnSendCheckCode').value='验证码发送中('+i+')';
t=setTimeout('FunCanUse()',1000);//1秒后启用
if(i==0)
{
clearTimeout(t);
i=10;
document.getElementById('btnSendCheckCode').value='重新发送验证码';
document.getElementById('btnSendCheckCode').disabled=false;//页面加载后禁用按钮
}
}
</script>
ASP.NET C# 页面代码:
<div>
手机号码:<input id="txtMoblie" type="text" />
<input id="btnSendCheckCode" type="button" οnclick="FunCanUse()" value="发送验证码" /></div>
<br />
<div>
接收验证码:<asp:TextBox ID="txtVertify" runat="server"></asp:TextBox>
<asp:Button ID="btnVertify" runat="server" Text="验证"
οnclick="btnVertify_Click" />
</div>
C#后台代码:
protected void btnVertify_Click(object sender, EventArgs e)
{
if (Request.Cookies["RemberCustom"] != null)
{
string strCheckCode = HttpUtility.UrlDecode(Request.Cookies["MobileRandCode"].Values[1].ToString());
string strCurDateTime = HttpUtility.UrlDecode(Request.Cookies["MobileRandCode"].Values[2].ToString());
//判断是否过期,若不过期,
DateTime dt=DateTime.Parse(strCurDateTime);
DateTime dtNow = System.DateTime.Now;
TimeSpan spT= dtNow - dt;
if (spT.TotalSeconds<=30)
{
if (strCheckCode == txtVertify.Text.Trim())
{
//执行数据库入库操作等等;
Response.Write("验证码正确!");
}
else
{
//执行数据库入库操作等等;
Response.Write("验证码错误!");
}
}
else
{
Response.Write("验证码过期,请重新获取!");
}
}
}
3、Ajax 代码(JS文件对应的 "/AjaxFile/MobileRandCode.ashx")
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string strMobile = HttpUtility.UrlEncode(context.Request.QueryString["mobile"]);
//通过手机短信接口发送数据,成功后则记录Cookie
//随机数
string RandData=GenerateRandomNumber(6);
HttpCookie cookie = new HttpCookie("MobileRandCode");
cookie.Values.Add("Mobile", strMobile);
cookie.Values.Add("CheckCode", RandData);
cookie.Values.Add("CurDateTime", System.DateTime.Now.ToString());
cookie.Expires.AddSeconds(30);
context.Response.AppendCookie(cookie);
}
private static char[] constant =
{
'0','1','2','3','4','5','6','7','8','9',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
};
public static string GenerateRandomNumber(int Length)
{
System.Text.StringBuilder newRandom = new System.Text.StringBuilder(62);
Random rd = new Random();
for (int i = 0; i < Length; i++)
{
newRandom.Append(constant[rd.Next(62)]);
}
return newRandom.ToString();
}