.NET MVC中如何使用手机验证码注册登陆
#region 手机验证码
/// <summary>
///
/// </summary>
/// <param name="yonghushouji">验证码的手机号</param>
/// <returns></returns>
public actionresult fayzm(string yonghushouji)
{
//产生随机验证码
random r = new random();
string temmsg = string.empty;
for (int i = 0; i < 6; i++)
{
temmsg += r.next(0, 9);
}
//保存随机码
dq.bll.sendyzmbll senbll = new dq.bll.sendyzmbll();
dq.model.sendyzm smodel = new dq.model.sendyzm();
smodel.shouji = yonghushouji;
smodel.yzm = temmsg;
smodel.guoqitime = datetime.now.addminutes(5);
senbll.add(smodel);
//post
string uid = "你所用的id";
string auth = "密匙";
string mobile = "" + yonghushouji + "";
string url = "测试开发商的网址?uid=" + uid + "&auth=" + auth + "&mobile=" + mobile + "&expid=0&msg=";
string content = "本次验证码为:" + temmsg + ""; //发送验证码的消息提示语句
system.text.encoding encode = system.text.encoding.getencoding("gbk");
content = httputility.urlencode(content, encode);
url += content;
console.writeline("get提交消息url:" + url);
string resp = creategethttpresponse(url);
console.writeline("响应:" + httputility.urldecode(resp, encode));
//console.readline();
return content("ok");
}
//验证输入的验证码
public actionresult checkyzm(string shuyzm, string yonghushouji)
{
dq.bll.personbll mbll = new dq.bll.personbll();
//先去数据库取数据
dq.bll.sendyzmbll senbll = new dq.bll.sendyzmbll();
var sylist = senbll.getlist(d => d.shouji == yonghushouji && d.guoqitime > datetime.now).tolist().orderbydescending(d => d.id).tolist();
var yzm = sylist[0].yzm;
//var dianpid = sylist[0].dianpuid;
if (!string.isnullorempty(yzm))
{
if (yzm == shuyzm)
{
var mlist = mbll.getlist(d => d.shoujihao == yonghushouji).tolist();
var dianpid = mlist[0].id;
return content("ok," + dianpid + "");
}
else
{
return content("验证码不正确");
}
}
else
{
return content("验证码失效请重新获取");
}
}
public static string creategethttpresponse(string url)
{
string strret = null;
if (url == null || url.trim().tostring() == "")
{
return strret;
}
string targeturl = url.trim().tostring();
try
{
httpwebrequest hr = (httpwebrequest)webrequest.create(targeturl);
hr.useragent = "mozilla/4.0 (compatible; msie 6.0; windows nt 5.1)";
hr.method = "get";
hr.timeout = 30 * 60 * 1000;
webresponse hs = hr.getresponse();
stream sr = hs.getresponsestream();
streamreader ser = new streamreader(sr, system.text.encoding.default);
strret = ser.readtoend();
console.writeline("<br/>get resp=" + strret);
}
catch (exception ex)
{
strret = null;
}
return strret;
}
#endregion
上一篇: java获取json中的全部键值对实例
下一篇: Java实现图片与二进制的互相转换
推荐阅读