.NET MVC中如何使用手机验证码注册登陆

2018-09-18 06:55:04来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

#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

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:最值得拥有的免费Bootstrap后台管理模板

下一篇:解决ASP.NET MVC 接受Request Playload参数问题