日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

一般处理程序制作的验证码

發(fā)布時間:2024/4/11 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一般处理程序制作的验证码 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、新建一個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());測試生成隨機驗證碼
?? //創(chuàng)建驗證圖片
??? 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("驗證碼錯了");
??????????? }
??????? }
??? }
}

四、圖片

五、源碼下載

轉(zhuǎn)載于:https://www.cnblogs.com/hao1234/archive/2011/08/14/2138561.html

總結(jié)

以上是生活随笔為你收集整理的一般处理程序制作的验证码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。