验证码验证
程序员文章站
2022-05-13 16:01:55
...
第一步:
打开VS 建立一个窗体
窗体名为ndex.aspx,使用验证码实现登录验证功能
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
账号:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
密码:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<br />
验证码:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:Image ID="Image1" ImageUrl="~/Handler1.ashx" runat="server" />
<asp:LinkButton ID="LinkButton1" runat="server">刷新</asp:LinkButton><br /><br />
<asp:Button ID="Button1" runat="server" Text="提交" OnClick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
第二步:
创建一个网站,并在网站中添加HTTPHandler类,继承IHttpHandler与IRequiresSessionState接口
右键项目 点击添加新建项目 找到一般应用程序
HTTPHandler类代码如下:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.SessionState;
namespace WebApplication2
{
/// <summary>
/// Handler1 的摘要说明
/// </summary>
public class Handler1 : IHttpHandler,IRequiresSessionState
{
//随机数对象
private Random RandomSeed = new Random();
public void ProcessRequest(HttpContext context)
{
//供验证码使用字符
string strWord = "23456789QWERTYUIOPASDFGHJKLZXCVBNM";
string NumStr = null;
for (int i = 0; i < 5; i++)
{
NumStr += strWord[RandomSeed.Next(0, strWord.Length)];
}
//将验证码保存到Session中
context.Session["vcode"] = NumStr.ToLower();
CreateImages(context, NumStr);
}
private void CreateImages(HttpContext context, string checkCode)
{
int iwidth = (int)(checkCode.Length * 13);
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 22);
Graphics g = Graphics.FromImage(image);
g.Clear(Color.White);
//定义颜色
Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Gray, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
//定义字体
string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };
Random rand = new Random();
//随机输出噪点
for (int i = 0; i < 5; i++)
{
int x = rand.Next(image.Width);
int y = rand.Next(image.Height);
g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1);
}
//输出不同字体和颜色的验证码字符
for (int i = 0; i < checkCode.Length; i++)
{
int cindex = rand.Next(7);
int findex = rand.Next(5);
Font f = new System.Drawing.Font(font[findex], 10, System.Drawing.FontStyle.Bold);
Brush b = new System.Drawing.SolidBrush(c[cindex]);
int ii = 4;
if((i+1)%2==0)
{
ii = 2;
}
g.DrawString(checkCode.Substring(i, 1), f, b, 2 + (i * 12), ii);
}
//画一个边框
g.DrawRectangle(new Pen(ColorTranslator.FromHtml("#CCCCCC"), 0), 0, 0, image.Width - 1, image.Height - 1);
//输出到浏览器
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
context.Response.ClearContent();
context.Response.ContentType = "image/gif";
context.Response.BinaryWrite(ms.ToArray());
g.Dispose();
image.Dispose();
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
第三步:
在Index界面写上button事件代码
账号:admin
密码:123456
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication2
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string account = TextBox1.Text;
string password = TextBox2.Text;
string code = TextBox3.Text;
if (Session["vcode"].ToString() == code.ToLower())
{
if (account == "admin" && password == "123456")
{
Response.Redirect("Index.aspx");
}
else
{
Label1.Text = "账号或密码不正确";
}
}
else {
Label1.Text = "验证码不正确";
}
}
}
}
然后看成果
验证码验证就这样完成了
上一篇: 短信验证码
下一篇: js 常见错误(待补充)