asp.net验证码
asp.net 生成验证码问题 .添加一个.ashx文件 <%@ WebHandler Language="C#" class="CheckCode" %> using System;
using System.Web;
using System.Drawing; public class CheckCode : IHttpHandler { public void ProcessRequest(HttpContext context)
{
string g = GenerateCheckCode();
CreateCheckCodeImage(g); } public bool IsReusable
{
get
{
return false;
}
} private string GenerateCheckCode()
{
int number;
char code;
string checkCode = String.Empty; System.Random random = new Random(); for (int i = ; i < ; i++)
{
number = random.Next(); if (number % == )
//生成'0'-'9'字符
code = (char)('' + (char)(number % ));
else
//生成'A'-'Z'字符
code = (char)('A' + (char)(number % )); checkCode += code.ToString();
} HttpContext.Current.Response.Cookies.Add(new HttpCookie("checkcode", checkCode)); return checkCode;
//两个字符相加等于=asicc码加
} private void CreateCheckCodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
return;
//(int)Math.Ceiling((checkCode.Length * 12.5)),22
System.Drawing.Bitmap image = new System.Drawing.Bitmap(, );
Graphics g = Graphics.FromImage(image); try
{
//生成随机生成器
Random random = new Random(); //清空图片背景色
g.Clear(Color.White); //画图片的背景噪音线
for (int i = ; i < ; 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+); Pen p = new Pen(Color.Gray, );
g.DrawLine(p, x1, y1, x2, y2);
} Font font = new System.Drawing.Font("Arial", , (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(, , image.Width, image.Height), Color.Blue, System.Drawing.Color.DarkRed, 1.2f, true);
g.DrawString(checkCode, font, brush, , ); //画图片的前景噪音点
for (int i = ; i < ; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height); image.SetPixel(x, y, System.Drawing.Color.FromArgb(random.Next()));
} //画图片的边框线
g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.Silver), , , image.Width - , image.Height - ); System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType = "image/jpg";
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
} } .在用到验证码的界面添加一个img控件,并且src属性指向.ashx文件 <img alt="看不清,换一张" src="CheckCode.ashx" onclick="this.src='CheckCode.ashx?abc='+Math.random()" /> .提交后先判断验证码是否正确 //检查验证码是否正确
if(this.txt_checkcode.Text.ToLower ()!=this.Request.Cookies["checkcode"].Value.ToLower () )
{
//写脚本提示
if( this.ClientScript.IsStartupScriptRegistered ("checkcode")==false)
{
this.ClientScript.RegisterStartupScript(this.GetType (),"checkcode","<script>alert('验证码错误!');</script>");
}
return;
}
asp.net验证码的更多相关文章
- ASP.net 验证码(C#) MVC
ASP.net 验证码(C#) MVC http://blog.163.com/xu_shuhao/blog/static/5257748720101022697309/ 网站添加验证码,主要为防止机 ...
- MVC的验证(模型注解和非侵入式脚本的结合使用) .Net中初探Redis .net通过代码发送邮件 Log4net (Log for .net) 使用GDI技术创建ASP.NET验证码 Razor模板引擎 (RazorEngine) .Net程序员应该掌握的正则表达式
MVC的验证(模型注解和非侵入式脚本的结合使用) @HtmlHrlper方式创建的标签,会自动生成一些属性,其中一些属性就是关于验证 如图示例: 模型注解 通过模型注解后,MVC的验证,包括前台客 ...
- Asp.Net验证码2
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System. ...
- 关于 ASP.NET 验证码
Session["CheckCode"] 这个..不懂神马意思.. .创建一个用户控件 用户名:TextBox 密码: TextBox 验证码:TextBox 验证码图片 < ...
- ASP.NET 验证码 不同浏览器 不刷新问题
具体为什么不刷新是缓存机制不同,验证码图片的src或ImageUrl的获取是来自一个文件,由于连接地址没变所以不同内核浏览器有的会认为源没有变,解决办法就是在连接后面加上一个随机参数如可以用JS的Ma ...
- asp.net验证码及怎么获取里面的数值(整合)
一.ASP.Net的验证码的作用 对于一个预防攻击的web表单来讲,验证码通常是一个常见的措施.因为如果对于一些public区域的页面内容来讲,譬如一个登录表单,如果没有必要的安全措施,很可能遭到模拟 ...
- ASP.NET——验证码的制作
我们在登陆站点,发表博客或者提交评论的时候,常常会遇到填写验证码这一项,当时感觉挺奇妙的样子,最终在牛腩新闻公布系统里接触到了,在这里小小的总结下. 用到的东东有三个: ...
- asp.net 验证码技术
网站验证码是一种很常用的技术.下面我介绍下技术上是如何实现的. 验证码是一张图片.我们需要在前台代码中写一段<img>,src指向一张页面(ValidateImage.aspx). < ...
- asp.net验证码的编写
很多时候我们在登录什么网站的时候,除了需要什么用户名和密码之外,有的还需要验证码那么在asp.net中这个验证码如何编写和设计,今天我就来给大家说一下: 首先创建一个页面名字随便起一个,我们这里叫做C ...
随机推荐
- 如何分析java内存泄漏问题
java中的内存泄漏首先需要dump文件出来,主要包括内存dump.线程dump: 内存dump是指通过jmap -dump <pid>输出的文件,而线程dump是指通过jstack &l ...
- 使用nginx实现负载均衡的配置
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #erro ...
- Win10系列:C#应用控件基础7
Slider控件 Slider控件包含一个滑动条.一个滑动块和一个取值范围,沿滑动条移动滑动块可以在取值范围内改变Slider控件的值.Slider控件的用途很广泛,例如可以使用Slider控件来设置 ...
- CCF关于公开NOIP复赛选手程序的通告
为使参加NOIP复赛的选手能了解和保存其竞赛时编制的程序,并相互监督,CCF责成各省特派员在复赛后公开选手程序. 公布方式:以提高组和普及组分别为单位,通过网站或邮件等方式公开.以每个选手可以看到本省 ...
- sublime3 前端个人常用插件及快捷键
首先先介绍如何启用插件安装功能: 打开Sublime 3,然后按 ctrl+` 或者在View → Show Console 在打开的窗口里黏贴这个网站上的代码(注意: Sublime 2和3所黏贴的 ...
- C# MVC 微信支付教程系列之公众号支付
微信支付教程系列之公众号支付 今天,我们接着讲微信支付的系列教程,前面,我们讲了这个微信红包和扫码支付.现在,我们讲讲这个公众号支付.公众号支付的应用环境常见的用户通过公众号,然后 ...
- LAMP架构(三)
第十九课 LAMP架构(三) 目录 一.配置防盗链 二.访问控制Directory 三.访问控制FilesMatch 四.限定某个目录禁止解析php 五.限制user_agent 六.php相关配置 ...
- poj1873(枚举+凸包)
The Fortified Forest Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 7291 Accepted: 2 ...
- python基础11_函数作用域_global_递归
看到了一个16进制转换的小知识点,就验证了一下运行结果. #!/usr/bin/env python # coding:utf-8 # 看到了16进制转换的问题.顺便验证一下. a = 255 b = ...
- 将文件夹中的图像路径自动生成txt文件(便于opencv遍历处理图像)
代码: #include<iostream> #include<vector> #include<io.h> #include<fstream> usi ...