asp.net验证码完整实例

本文提供了一个ASP.NET验证码的完整实现,包括代码和页面布局。通过后台生成随机验证码并保存到SESSION中,前端通过按钮点击事件刷新验证码。用户输入验证码后,后台进行验证,实现了简单的验证码功能。
摘要由CSDN通过智能技术生成

 验证码页面(代码全在后台):

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebApplication2
{
 /// <summary>
 /// WebForm1 的摘要说明。
 /// </summary>
 public class WebForm1 : System.Web.UI.Page
 {
  // 验证码长度
  private int codeLen = 4;
  // 图片清晰度
  private int fineness =90;
  // 图片宽度
  private int imgWidth = 55;
  // 图片高度
  private int imgHeight = 24;
  // 字体家族名称
  private string fontFamily = "Times New Roman";
  // 字体大小
  private int fontSize = 14;
  // 字体样式
  private int fontStyle = 0;
  // 绘制起始坐标 X
  private int posX = 0;
  protected System.Web.UI.WebControls.Button Button1;
  // 绘制起始坐标 Y
  private int posY = 0;
  private void Page_Load(object sender, EventArgs e)
  {
   //读取 Request 传递参数#region 读取 Request 传递参数
   // 获取代码长度设置
   if (Request["CodeLen"] != null)
   {
    try
    {
     codeLen = Int32.Parse(Request["CodeLen"]);

    // 规定验证码长度
     if (codeLen < 4 || codeLen > 16)
      throw new Exception("验证码长度必须在4

到16之间");
    }
    catch (Exception ex)
    {
     throw ex;
    }
   }

   // 获取图片清晰度设置
   if (Request["Fineness"] != null)
   {
    try
    {
     fineness = Int32.Parse(Request["Fineness"]);

     // 验证清晰度
     if (fineness < 0 || fineness > 100)
      throw new Exception("图片清晰度必须在0

到100之间");
    }
    catch (Exception Ex)
    {
     throw Ex;
    }
   }

   // 获取图片宽度
   if (Request["ImgWidth"] != null)
   {
    try
    {
     imgWidth = Int32.Parse(Request["ImgWidth"]);

     if (imgWidth < 16 || imgWidth > 480)
      throw new Exception("图片宽度必须在16到

480之间");
    }
    catch (Exception Ex)
    {
     throw Ex;
    }
   }

   // 获取图片高度
   if (Request["ImgHeight"] != null)
   {
    try
    {
     imgWidth = Int32.Parse(Request["ImgHei

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值