一般处理程序制作的验证码
一、新建一個ValidateCode.ashx文件
<%@ WebHandler Language="C#" Class="ValidateCode"Debug="true" %>
//Debug="true"是加上去的
using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web.SessionState;//要使用Session必須使用命名空間里的接口IRequiresSessionState
public class ValidateCode : IHttpHandler,IRequiresSessionState{
????? public void ProcessRequest (HttpContext context) {
??????? context.Response.ContentType = "image/jpeg";
??????? //context.Response.Write("Hello World"+MakeRandomNum());測試生成隨機驗證碼
?? //創建驗證圖片
??? string vCode=MakeRandomNum();
??? context.Session["vcode"] = vCode;//給Session里寫驗證碼值
??? using (Bitmap img = new Bitmap(80,30))
????? {
?????? using (Graphics g = Graphics.FromImage(img))
??????? {
????????? g.DrawString(vCode,new Font("微軟雅黑",16),Brushes.White,10,2);
????????? img.Save(context.Response.OutputStream,ImageFormat.Jpeg);
???????? }
?????? }
????? }
??? //生成隨機驗證碼
??? public string MakeRandomNum()
??? {
??????? Random ran = new Random();
??????? string resNum = string.Empty;
??????? for (int i = 0; i < 4; i++)
??????? {
??????????? resNum += ran.Next(9).ToString();
??????? }
??????? return resNum;
??? }
????? public bool IsReusable {
??????? get {
??????????? return false;
???????????? }
?????? }
}
二、登陸頁面C05LoginBySession.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="C05LoginBySession.aspx.cs" Inherits="C05LoginBySession" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
??? <title></title>
</head>
<body>
??? <form id="form1" action="C05LoginBySession.aspx"method="post">
??? <input id="txtName"name="txtName"type="text"/><br/>
??? <input id="txtPwd"name="txtPwd"type="text"/><br/>
??? <input id="txtCode"name="txtCode"type="text"/><img src="ValidateCode.ashx"/><br/>
??? <input id="Submit1"type="submit"value="登陸"/>
??? <a href="Reg.aspx">注冊</a>
??? <input type="hidden"name="IsPostBack"value="1"/>
??? </form>
</body>
</html>
三、登陸頁面C05LoginBySession.aspx.cs? 到Session讀值判斷驗證碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class C05LoginBySession : System.Web.UI.Page
{
??? protected void Page_Load(object sender, EventArgs e)
??? {
??????? if (!string.IsNullOrEmpty(Request.Form["IsPostBack"]))
??????? {
??????????? string strCode=Request.Form["txtCode"];
??????????? if (!string.IsNullOrEmpty(strCode) && Session["vcode"] != null && strCode == Session["vcode"].ToString())
??????????? {
??????????????? string strName = Request.Form["txtName"];
??????????????? string strPwd = Request.Form["txtPwd"];
??????????????? Session["uname"] = strName;
??????????????? Session["udate"] = DateTime.Now.ToString();
??????????????? Response.Redirect("IndexBySession.aspx");
??????????? }
??????????? else
??????????? {
??????????????? Response.Write("驗證碼錯了");
??????????? }
??????? }
??? }
}
四、圖片
五、源碼下載
轉載于:https://www.cnblogs.com/hao1234/archive/2011/08/14/2138561.html
總結
以上是生活随笔為你收集整理的一般处理程序制作的验证码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#调C++生成的dll报0x80073
- 下一篇: dns的正反向解析