日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

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

一、新建一個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

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。