winform之判断验证码,,附加验证码的一般处理程序
这里用的一般处理程序画的验证码图片。
判断验证码步骤:
①先在一般处理程序中获取验证码(一般处理程序中session的调用→context.Session[])
- string code = GetRndStr();
- //将验证码存入session中
- context.Session["yzm"] = code;
②在一般处理程序中使用session必须实现一个接口,接口的命名空间为using System.Web.SessionState;
接口名:IRequiresSessionState
③再判断验证码时要以防万一,,session的生命周期,所以先判断Session是否为空,不是空再继续以后的判断
- ToLower()或者ToUpper()可以使验证码大小写不敏感
- //一,先判断验证码
- if (txtYZM.Text.Trim().ToLower() == Session["yzm"].ToString().ToLower() && Session["yzm"]!=null)
- {
- //判断验证码成功后,删除session
- Session.Remove("yzm");
- //二,再判断账号密码
一般处理程序中的验证码
- using System.Web.SessionState;
- public class ValidateCode : IHttpHandler,IRequiresSessionState {
- public void ProcessRequest(HttpContext context)
- {
- context.Response.ContentType = "image/jpeg";
- string code = GetRndStr();
- //将验证码存入session中
- context.Session["yzm"] = code;
- using (Bitmap img = CreateImages(code, "ch"))
- {
- img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
- }
- }
- public bool IsReusable
- {
- get
- {
- return false;
- }
- }
- /// <summary>
- /// 数字随机数
- /// </summary>
- /// <returns></returns>
- private string GetRndNum()
- {
- string code = string.Empty;
- Random random = new Random();
- ; i < ; i++)
- {
- code += random.Next();
- }
- return code;
- }
- /// <summary>
- /// 英文随机
- /// </summary>
- /// <returns></returns>
- private string GetRndStr()
- {
- string Vchar = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
- string[] VcArray = Vchar.Split(',');
- string checkCode = string.Empty;
- Random rand = new Random();
- ; i < ; i++)
- {
- int t = rand.Next(VcArray.Length);
- checkCode += VcArray[t];
- }
- return checkCode;
- }
- /// <summary>
- /// 中文随机
- /// </summary>
- /// <returns></returns>
- private string GetRndCh()
- {
- System.Text.Encoding gb = System.Text.Encoding.Default;//获取GB2312编码页(表)
- );//生4个随机中文汉字编码
- ];
- System.Text.StringBuilder sb = new System.Text.StringBuilder();
- ; i < ; i++)
- {
- //根据汉字编码的字节数组解码出中文汉字
- str[i] = gb.GetString((byte[])Convert.ChangeType(bytes[i], typeof(byte[])));
- sb.Append(str[i].ToString());
- }
- return sb.ToString();
- }
- /// <summary>
- /// 产生随机中文字符
- /// </summary>
- /// <param name="strlength"></param>
- /// <returns></returns>
- private static object[] CreateRegionCode(int strlength)
- {
- //定义一个字符串数组储存汉字编码的组成元素
- ] { ", "a", "b", "c", "d", "e", "f" };
- Random rnd = new Random();
- object[] bytes = new object[strlength];
- ; i < strlength; i++)
- {
- //区位码第1位
- , );
- string str_r1 = rBase[r1].Trim();
- //区位码第2位
- rnd = new Random(r1 * unchecked((int)DateTime.Now.Ticks) + i);
- int r2;
- )
- {
- r2 = rnd.Next(, );
- }
- else
- {
- r2 = rnd.Next(, );
- }
- string str_r2 = rBase[r2].Trim();
- //区位码第3位
- rnd = new Random(r2 * unchecked((int)DateTime.Now.Ticks) + i);//更换随机种子
- , );
- string str_r3 = rBase[r3].Trim();
- //区位码第4位
- rnd = new Random(r3 * unchecked((int)DateTime.Now.Ticks) + i);
- int r4;
- )
- {
- r4 = rnd.Next(, );
- }
- )
- {
- r4 = rnd.Next(, );
- }
- else
- {
- r4 = rnd.Next(, );
- }
- string str_r4 = rBase[r4].Trim();
- //定义两个字节变量存储产生的随机汉字区位码
- );
- );
- //将两个字节变量存储在字节数组中
- byte[] str_r = new byte[] { byte1, byte2 };
- //将产生的一个汉字的字节数组放入object数组中
- bytes.SetValue(str_r, i);
- }
- return bytes;
- }
- /// <summary>
- /// 画图片的背景图+干扰线
- /// </summary>
- /// <param name="checkCode"></param>
- /// <returns></returns>
- private Bitmap CreateImages(string checkCode, string type)
- {
- ;
- if (type == "ch")
- {
- step = ;//中文字符,边界值做大
- }
- + step));
- System.Drawing.Bitmap image = );
- Graphics g = Graphics.FromImage(image);
- g.Clear(Color.White);//清除背景色
- Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };//定义随机颜色
- string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };
- Random rand = new Random();
- ; i < ; i++)
- {
- int x1 = rand.Next(image.Width);
- int x2 = rand.Next(image.Width);
- int y1 = rand.Next(image.Height);
- int y2 = rand.Next(image.Height);
- g.DrawLine(), x1, y1, x2, y2);//根据坐标画线
- }
- ; i < checkCode.Length; i++)
- {
- );
- );
- Font f = , System.Drawing.FontStyle.Bold);
- Brush b = new System.Drawing.SolidBrush(c[cindex]);
- ;
- ) % == )
- {
- ii = ;
- }
- g.DrawString(checkCode.Substring(i, ), f, b, + (i * ( + step)), ii);
- }
- g.DrawRectangle(), , , image.Width - , image.Height - );
- System.IO.MemoryStream ms = new System.IO.MemoryStream();
- return image;
- }
- }
winform之判断验证码,,附加验证码的一般处理程序的更多相关文章
- C# WinForm 使用SMS接口发送手机验证码+图形验证码+IP限制
https://blog.csdn.net/IT_xiao_guang_guang/article/details/104299983 前言 1.发送手机验证码用的是网建的SMS接口(http:/ ...
- Js 判断输入的验证码是否一致
实现效果: 判断输入的验证码是否一致 如果不同,alert出验证码输入有误~, 输入正确输出登录成功. <!DOCTYPE html> <html lang="en&quo ...
- 简单说说Spring Security 使用(附加验证码登录,自定义认证)
先看官方文档:http://docs.spring.io/spring-security/site/docs/4.0.x/reference/htmlsingle/ spring security4已 ...
- c#winform图片绘制与图片验证码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 《新手养成记》--第一篇 iOS手机号正则判断和获取验证码
今天是开通一个新博客开启新的一年,同时也是对自己的过去做一个告别.介绍一下博主自己,资深iOS屌丝男,今天写这个博客就是刻意拉低逼格的,前两年写的博客为了装那什么,故意写的高大上,其实呵呵哒...年假 ...
- java验证码-汉字验证码
今天整理了一个java实现的汉字输入验证码 主要包含两个类,一个是生成验证码,一个是判断验证码输入是否正确 实现原理非常简单,将汉字和干扰线生成图片并将汉字保存到session,前台获取每次生成验证码 ...
- C# Ajax 手机发送短信验证码 校验验证码 菜鸟级别实现方法
1.Ajax请求处理页面: using System; using System.Collections.Generic; using System.Linq; using System.Web; u ...
- php生成动态验证码 加减算法验证码 简单验证码
预览效果: <?php /** *ImageCode 生成包含验证码的GIF图片的函数 *@param $string 字符串 *@param $width 宽度 *@param $height ...
- Java Swing 图形界面实现验证码(验证码可动态刷新)
import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Toolkit;import j ...
随机推荐
- 对"构建之法“的理解和困惑
对"构建之法"的理解和困惑 本人"学沫沫"一个,对于之前的编程学习虽不大"感冒",但秉着对自己负责的态度进行了基础学习. ...
- gunplot demo
//author : Leon yangli0534@gmail.com #include <stdlib.h> #include <stdio.h> #include < ...
- document.write和innerHTML的区别
document.write是直接写入到页面的内容流,如果在写之前没有调用document.open, 浏览器会自动调用open.每次写完关闭之后重新调用该函数,会导致页面被重写. innerHTML ...
- tomcat配置jenkins遇到的问题
在执行jenkinks时,遇到以下错误: 原因:未在tomcat/conf中的tomcat-users.xml中配置用户 解决方法:在tomcat/conf/tomcat-users.xml中添加以下 ...
- Linux下php安装memcache扩展
安装环境:CentOS 6.4 php扩展memcache的作用是为了支持memcached数据库缓存服务器,下面是安装方法. 1.下载 下载地址:http://pecl.php.net/packag ...
- 11Spring_AOP编程(AspectJ)_概述
AspectJ 是一个框架 (第三方AOP框架 ),提供切面编程 ,编写一个Aspect 支持多个Advice和多个PointCut .对比前一种提到的传统的Aop编程,AspctJ更加的常用.Asp ...
- 解决 php-cgi 启动时提示缺少 msvcr110.dll 的问题
问题描述: 启动 php-cgi 时如果提示缺少 msvcr110.dll 问题原因: 缺少 vc 2012 运行库 问题解决: 需要安装 vcredist_x64.exe 或 vcredist_ ...
- 转载:有关SQL server connection KeepAlive 的FAQ
转:http://blogs.msdn.com/b/apgcdsd/archive/2011/05/03/sql-server-connection-keepalive-faq.aspx 1.什么是S ...
- 什么是json
http://www.ruanyifeng.com/blog/2009/05/data_types_and_json.html http://edu.51cto.com/lesson/id-71123 ...
- CentOS7 SSH相关
1.查看SSH是否已经安装,命令: rpm -qa |grep ssh 如果列出了openssh-x.x开头的软件名,代表已经安装 如果没有安装,使用命令安装: yum install ssh 2.如 ...