资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

怎么在asp.net中利用ashx生成一个图形验证码-创新互联

本篇文章为大家展示了怎么在asp.net中利用ashx生成一个图形验证码,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

目前创新互联建站已为成百上千的企业提供了网站建设、域名、虚拟主机、网站托管、服务器托管、企业网站设计、回民网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

1. 写个ashx生成图形验证码


using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Web.SessionState;
using System.Drawing;
namespace usechecknum.ashx
{
  /// 
  /// $codebehindclassname$ 的摘要说明
  /// 
  [WebService(Namespace = "http://tempuri.org/")]
  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  public class doCreateNum : IHttpHandler,IRequiresSessionState
  {
    public void ProcessRequest(HttpContext context)
    {
      context.Response.ContentType = "text/html";
      string checkCode = GetValidation(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();
        //清空图片背景色
        g.Clear(Color.White);
        // 画图片的背景噪音线
        int i;
        for (i = 0; i < 25; i++)
        {
          int x1 = random.Next(image.Width);
          int x2 = random.Next(image.Width);
          int y1 = random.Next(image.Height);
          int y2 = random.Next(image.Height);
          g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
        }
        Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold));
        System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2F, true);
        g.DrawString(checkCode, font, brush, 2, 2);
        //画图片的前景噪音点
        g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
        context.Response.ClearContent();
        context.Response.ContentType = "image/Gif";
        context.Response.BinaryWrite(ms.ToArray());
      }
      finally
      {
        g.Dispose();
        image.Dispose();
      }
    }
    public string GetValidation(int num)
    {
      string str = "/upload/otherpic43/ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //"或者写汉字也行"
      string validatecode = "";
      Random rd = new Random();
      for (int i = 0; i < num; i++)
      {
        validatecode += str.Substring(rd.Next(0, str.Length), 1);
      }
      return validatecode;
    }
    public bool IsReusable
    {
      get
      {
        return false;
      }
    }
  }
}

2. 在页面上显示验证码,因为我们生成的是图形,所以可以直接写在标签里,我们只要写段简单的脚本就可以点击鼠标切换验证码了

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="usechecknum._Default" %>



  验证码的使用


  function changeCode() {
    var imgNode = document.getElementById("vimg");
    imgNode.src = "ashx/doCreateNum.ashx?t=" + (new Date()).valueOf(); // 这里加个时间的参数是为了防止浏览器缓存的问题
  }
  

  
   请输入验证码:
  

说了半天,该是时候看看生成的验证码长什么样了

怎么在asp.net中利用ashx生成一个图形验证码

上述内容就是怎么在asp.net中利用ashx生成一个图形验证码,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。


网站栏目:怎么在asp.net中利用ashx生成一个图形验证码-创新互联
链接地址:http://cdkjz.cn/article/ddcppj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220