admin管理员组文章数量:1656248
网页版登录。https://blog.csdn/wybshyy/article/details/103121598
WebService.ashx
<%@ WebHandler Language="C#" Class="WebService" %>
using System;
using System.Text;
using System.Web;
using Model;
using BLL;
using System.Drawing;
using System.IO;
using System.Net;
public class WebService : IHttpHandler
{
//示例http://localhost:2446/WebService.ashx?action=GetEmployNO&phone_mobile=admin
private int counter;
private static readonly object synclock = new object();
CommonBLL bll = new CommonBLL();
private string uid = "czxkd";//短信平台用户名
private string key = "******";//密钥
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
string action = context.Request["action"];
switch (action)
{
case "Register": //注册
Register(context);
context.Response.End();
break;
case "getCode": //获取验证码
getCode(context);
context.Response.End();
break;
case "userLogin": //登录
userLogin(context);
context.Response.End();
break;
case "integralDetails": //新增积分记录
integralDetails(context);
context.Response.End();
break;
case "getBanner": //获取banner
getBanner(context);
context.Response.End();
break;
case "getRecommendedActivities": //获取推荐活动
getRecommendedActivities(context);
context.Response.End();
break;
case "getsceneryListByType": //根据景区获取其中景点列表
getsceneryListByType(context);
context.Response.End();
break;
case "addClockRecord": //新增打卡记录
addClockRecord(context);
context.Response.End();
break;
case "GetEmployNO": //根据手机号
GetEmployNO(context);
context.Response.End();
break;
case "GetQuestList": //根据工号获取未过期的问卷
GetQuestList(context);
context.Response.End();
break;
default:
break;
}
}
#region 注册
//示例http://localhost:2446/WebService.ashx?action=Register&username=name01&pwd=123456&email=11qq&qq=111222&mobile=13888888888
private void Register(HttpContext context)
{
BLL.user bll = new BLL.user();
Model.user model = new Model.user();
model.username = context.Request["username"];
model.pwd = context.Request["pwd"];
model.email = context.Request["email"];
model.qq = context.Request["qq"];
model.mobile = context.Request["mobile"];
context.Response.Write(bll.Register(model));
}
#endregion
#region 先获取验证码,再登录
//示例http://localhost:2446/WebService.ashx?action=getCode
private void getCode(HttpContext context)
{
//从http://sms.webchinese/申请账号,获得密钥
string vc = "";//生成4位随机数码做为验证码
Random r = new Random();
int num1 = r.Next(0, 9);
int num2 = r.Next(0, 9);
int num3 = r.Next(0, 9);
int num4 = r.Next(0, 9);
int[] numbers = new int[4] { num1, num2, num3, num4 };
for (int i = 0; i < numbers.Length; i++)
{
vc += numbers[i].ToString();
}
string number = "15805910204";//接受短信的手机号
string smsText = "您的验证码为" + vc + "。";//【重走霞客路】
string postUrl = GetPostUrl(number, smsText);
string result = PostSmsInfo(postUrl);
string t = GetResult(result);
HttpCookie mycookie = new HttpCookie("getCode");
mycookie.Values.Add("VCode", vc);//把验证码放到COOKIE里
TimeSpan ts = new TimeSpan(3, 0, 30, 0);
DateTime dt = DateTime.Now;
mycookie.Expires = dt.Add(ts);
context.Response.AppendCookie(mycookie);
}
//示例http://localhost:2446/WebService.ashx?action=userLoginmobile=13888888888&VCode=name01
private void userLogin(HttpContext context)
{
BLL.user bll = new BLL.user();
Model.user model = new Model.user();
model.VCode = context.Request["VCode"];//验证码
model.mobile = context.Request["mobile"];//手机号
//先验证手机号是否存在。再验证验证码是否正确
HttpCookie MyCookie = context.Request.Cookies["getCode"];
string CookieCode = Convert.ToString(MyCookie.Values["VCode"]);//获取存在COOKIE里的验证码,
if (model.VCode == CookieCode)
{
context.Response.Write(bll.Register(model));
}
else
{
context.Response.Write("");
}
}
public string GetPostUrl(string smsMob, string smsText)
{
//uid为用户名,key为密钥
string postUrl = "http://utf8.api.smschinese/?Uid=" + uid + "&key=" + key + "&smsMob=" + smsMob + "&smsText=" + smsText;
return postUrl;
}
public string PostSmsInfo(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;MISE 6.0;Window NT 5.1)";
hr.Method = "GET";
hr.Timeout = 30 * 60 * 1000;
WebResponse hs = hr.GetResponse();
Stream sr = hs.GetResponseStream();
StreamReader ser = new StreamReader(sr, Encoding.Default);
strRet = ser.ReadToEnd();
}
catch (Exception ex)
{
strRet = null;
}
return strRet;
}
public string GetResult(string strRet)
{
int result = 0;
try
{
result = int.Parse(strRet);
switch (result)
{
case -1:
strRet = "没有该用户账户";
break;
case -2:
strRet = "接口密钥不正确,不是账户登陆密码";
break;
case -21:
strRet = "MDS接口密钥加密不正确";
break;
case -3:
strRet = "短信数量不足";
break;
case -11:
strRet = "该用户被禁用";
break;
case -14:
strRet = "短信内容出现非法字符";
break;
case -4:
strRet = "手机格式不正确";
break;
case -41:
strRet = "手机号码为空";
break;
case -42:
strRet = "短信内容为空";
break;
case -51:
strRet = "短信签名格式不正确,接口签名格式为:【签名内容】";
break;
case -6:
strRet = "IP限制";
break;
default:
strRet = "发送短信数量:" + result;
break;
}
}
catch (Exception ex)
{
strRet = ex.Message;
}
//TextBox1.Text = strRet.ToString();
return strRet;
}
#endregion
#region 新增积分记录
//示例http://localhost:2446/WebService.ashx?action=integralDetails&integralNum=1&integralSource=签到积分&remarks=签到积分
private void integralDetails(HttpContext context)
{
integralDetailsBLL bll = new integralDetailsBLL();
integralDetails model = new integralDetails();
model.IntegralNum = Convert.ToInt32(context.Request["integralNum"]);
model.IntegralSource = context.Request["integralSource"];
model.Remarks = context.Request["remarks"];
context.Response.Write(bll.insert(model));
}
#endregion
#region 获取banner
//示例http://localhost:2446/WebService.ashx?action=getBanner
private void getBanner(HttpContext context)
{
context.Response.Write(bll.getBanner());
}
#endregion
#region 获取推荐活动
//示例http://localhost:2446/WebService.ashx?action=getRecommendedActivities
private void getRecommendedActivities(HttpContext context)
{
context.Response.Write(bll.getRecommendedActivities());
}
#endregion
#region 根据景区获取其中景点列表
//示例http://localhost:2446/WebService.ashx?action=getsceneryListByType&typeId=1
private void getsceneryListByType(HttpContext context)
{
int typeId = Convert.ToInt32(context.Request["typeId"]);
if (typeId > 0)
{
context.Response.Write(bll.getsceneryListByType(typeId));
}
}
#endregion
#region 新增打卡记录
//示例http://localhost:2446/WebService.ashx?action=addClockRecord&userid=111&sceneryListid=222
private void addClockRecord(HttpContext context)
{
lock (synclock)
{
clockRecord model = new clockRecord();
model.Userid = Convert.ToInt32(context.Request["userid"]);//用户ID
model.SceneryListid = Convert.ToInt32(context.Request["sceneryListid"]);//扫码的景点ID
string strResult = bll.addClockRecord(model);
if (strResult.IndexOf("发证书") > 0)
{
creatCertificate("用户名");
}
context.Response.Write(strResult);
}
}
#endregion
#region 根据手机号码,返回工号
private void GetEmployNO(HttpContext context)
{
BLL.user bll = new BLL.user();
string phone_mobile = context.Request["phone_mobile"];
if (phone_mobile == null) { phone_mobile = ""; }
context.Response.Write(bll.GetEmployNO(phone_mobile));
}
#endregion
#region 根据工号获取未过期的问卷
private void GetQuestList(HttpContext context)
{
BLL.user bll = new BLL.user();
string employ_no = context.Request["employ_no"];
if (employ_no == null) { employ_no = ""; }
context.Response.Write(bll.GetQuestList(employ_no));
}
#endregion
#region 生成证书
public void creatCertificate(string username)
{
//画布的宽度和高度
int initialWidth = 399, initialHeight = 283;
Bitmap theBitmap = new Bitmap(initialWidth, initialHeight);
Graphics theGraphics = Graphics.FromImage(theBitmap);
//呈现质量
theGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
//背景色
theGraphics.Clear(Color.FromArgb(255, 255, 255));
//灰色边框
theGraphics.DrawRectangle(new Pen(Color.FromArgb(227, 227, 227), 1), 1, 1, initialWidth - 2,
initialHeight - 2);
int autoHeight = 0; //累计高度(动态的内容高度位置)
int sjh = 0; //时间轴的高度
float fwigth = 0;
int fontHeight = 12; //文字行间距
string FontType = "微软雅黑";
string IconType = "iconfont";
Font theFont = new Font(FontType, 12.0f, System.Drawing.FontStyle.Bold);
//准备工作。定义画刷颜色
Color col = Color.FromArgb(51, 51, 51);
Brush newBrush = new SolidBrush(col);
Brush bush = new SolidBrush(Color.FromArgb(204, 204, 204)); //填充的颜色
Bitmap bmp = Get_img(@"http://localhost:2446/aadmin/images/zhengshu.jpg");//背景图片地址~/aadmin/images/zhengshu.jpg
//画布的开始坐标和结束坐标
theGraphics.DrawImage(bmp, new System.Drawing.Rectangle(0, 0, 399, 283),
new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height),
System.Drawing.GraphicsUnit.Pixel);
//文字居画布的位置坐标
theGraphics.DrawString(username, theFont, newBrush, 90, 50);//username为传来的动态文字
//Bitmap转byte[]
MemoryStream ms = new MemoryStream();
theBitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] bytes = ms.GetBuffer();
ms.Close();
string imurl = "/aadmin/userImages/" + DateTime.Now.ToFileTime().ToString() + ".Png";
MemoryStream stream = new MemoryStream(bytes);
System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
//要存到相对路径要用HttpContext.Current.Server.MapPath。否则只能用绝对路径,E:/这种格式
img.Save(HttpContext.Current.Server.MapPath(imurl), System.Drawing.Imaging.ImageFormat.Png);
}
public Bitmap Get_img(string imgurl)
{
Bitmap img = null;
HttpWebRequest req;
HttpWebResponse res = null;
try
{
System.Uri httpUrl = new System.Uri(imgurl);
req = (HttpWebRequest)(WebRequest.Create(httpUrl));
req.Timeout = 180000; //设置超时值10秒
req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0";
req.Accept = "text/html, application/xhtml+xml, */*";
req.Method = "GET";
res = (HttpWebResponse)(req.GetResponse());
img = new Bitmap(res.GetResponseStream());//获取图片流
}
catch (Exception e)
{
}
return img;
}
#endregion
public bool IsReusable
{
get
{
return false;
}
}
}
版权声明:本文标题:C# asp.net手机验证码登录,获取验证码(接口版) 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1729732543a1211531.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论