.NET生成验证码

  在很多网站中,不免少不了输入验证码这项操作,为什么要输入验证码呢?一是防止自动批量注册,二是防止暴力破解。那验证码是怎么做成的?请往下看~

第一步:添加“一般处理程序”;
在这里插入图片描述
第二步:编写“代码”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Web.SessionState;

namespace WebApplication.handler
{
   
    /// <summary>
    /// WaterMark 的摘要说明
    /// </summary>
    public class WaterMark : IHttpHandler, IRequiresSessionState  // 要使用session必须实现该接口,记得要导入System.Web.SessionState命名空间
    {
   

        public void ProcessRequest(HttpContext context)
        {
   
            string checkCode = GenCode(5);  // 产生5位随机字符
            context.Session["Code"] = checkCode; //将字符串保存到Session中,以便需要时进行验证
            System.Drawing.Bitmap image = new System.Drawing.Bitmap(70, 22);
            Graphics g = Graphics.FromImage(image);
            try
            {
   
                //生成随机生成器
                Random random = new Random
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值