新建ASP.NET Web应用程序WebYzm
然后在WebYzm里面添加一般处理程序HttpHandler
然后来继承IHttpHandler与IRequiresSessionState接口 然后在Handler1.ashx里面编写以下代码
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.SessionState;
namespace WebYzm
{
/// <summary>
/// Handler1 的摘要说明
/// </summary>
public class Handler1 : IHttpHandler,IRequiresSessionState
{
private Random RandomSeed = new Random();
public void ProcessRequest(HttpContext context)
{
//供验证码使用字符
string strWord = "23456789QWERTYUPASDFGHKXCVBNM";
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.Green, 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 < 50; 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;
}
}
}
}
然后添加一个Web窗体Index 使用验证码实现登录验证功能 代码如下
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="WebYzm.Index" %>
<!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 id="app">
<table border="1">
<tr>
<td>账号:</td>
<td><asp:TextBox ID="txtAccount" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>密码:</td>
<td><asp:TextBox ID="txtPwd" runat="server" TextMode="Password"></asp:TextBox></td>
</tr>
<tr>
<td>验证码</td>
<td>
<asp:TextBox ID="txtCode" runat="server"></asp:TextBox>
<asp:Image ID="Image1" ImageUrl="Handler1.ashx" runat="server" />
<asp:LinkButton ID="LinkButton1" runat="server">刷新</asp:LinkButton>
</td>
</tr>
</table>
<asp:Button ID="btnSubmit" runat="server" Text="提交" OnClick="btnSubmit_Click" />
</div>
<asp:Label ID="lblMessge" runat="server" Text=""></asp:Label>
</form>
</body>
</html>
窗体样式代码如下
<style>
*{
margin:0 auto;
}
#app{
width:600px;
margin:50px auto;
}
table,tr,td{
border-collapse: collapse;
border-spacing: 0;
}
table{
width: 100%
}
tr,td{
border: 1px solid #bcbcbc;
padding: 5px 10px;
}
tr{
background-color: #42b983;
font-size: 1.2rem;
font-weight: 400;
color: #fff;
cursor: pointer;
}
#btnSubmit{
border:none;
width:100px;
height:35px;
background-color: #82c8a0;
color:white;
box-shadow: 0 0 0 1px #82c8a0 inset,
0 0 0 2px rgba(255,255,255,0.15) inset,
0 8px 0 0 rgba(126, 194, 155, .7),
0 8px 0 1px rgba(0,0,0,.4),
0 8px 8px 1px rgba(0,0,0,0.5);
}
#btnSubmit:active {
box-shadow: 0 0 0 1px #82c8a0 inset,
0 0 0 2px rgba(255,255,255,0.15) inset,
0 0 0 1px rgba(0,0,0,0.4);
}
#LinkButton1{
color:black;
}
</style>
然后再新建一个窗体Index2来显示登录成功
<body>
<form id="form1" runat="server">
<div>
<h1>登录成功</h1>
</div>
</form>
</body>
然后在Index.aspx.cs中编写后台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebYzm
{
public partial class Index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
string account = txtAccount.Text;
string password = txtPwd.Text;
string code = txtCode.Text;
if (Session["vcode"].ToString()==code.ToLower())
{
if (account=="admin"&&password=="123456")
{
Response.Redirect("Index2.aspx");
}
else
{
lblMessge.Text = "账号或密码不正确";
}
}
else
{
lblMessge.Text = "验证码不正确!";
}
}
}
}
然后我们在WebYzm中新建一个image文件夹 存储验证码背景图片
然后我们运行
就成功了!