.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

posted @ 2018-09-12 10:31 忙碌的蜗牛 阅读(...) 评论(...) 编辑 收藏