asp.net 验证码(一)Session
1.模板页 //创建网页模板 输入验证码文本框 并且将文本框中的内容发送的后端验证中去
<p>请输入验证码:<input type="text" name="checkcode" /><img src="/createcheckcode.ashx"/></p> //输入名字及和地址
2.服务端
引用命名空间 并且将响应的类型修改成图片形式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Web.SessionState;
namespace WebApplication3
{
/// <summary>
/// createcheckcode 的摘要说明
/// </summary>
public class createcheckcode : IHttpHandler, IRequiresSessionState //实现ashx
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/JPEG";//相应的类型格式
Bitmap map = new Bitmap(80,30);//相应的宽高
Graphics g = Graphics.FromImage(map);//基于这个图片拿到这个绘图对象
g.FillRectangle(Brushes.White, 1, 1, 78, 28);//画笔颜色为白色、并且从1,1点开始,后面是宽度
char[] code = new char[26];//定义26个字母并且将字母存储在code中
for (int i = 0; i < 26; i++)
{
code[i] = (char)('a' + i);//26个字母开始循环
}
string checkdode = ""; //定义随机数
Random r = new Random(); //创建随机函数
for (int i = 0; i < 4; i++)
{
checkdode = checkdode + code[r.Next(0, 26)]; //循环将字母写入
}
context.Session["checkdode"] = checkdode;//存储一份
g.DrawString(checkdode, new Font("黑体", 20), Brushes.Black, new PointF(2, 2));//将随机后随机绘画在图像之中
map.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);//响应回去的图片类型
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;
namespace WebApplication3
{
/// <summary>
/// dl 的摘要说明
/// </summary>
public class dl : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
string name = context.Request["name"];
string password = context.Request["password"];
string c = context.Request["checkdode"];//从请求接收数据
string html = File.ReadAllText(context.Server.MapPath("/dl.html"));
if (string.IsNullOrEmpty(name))
{
context.Response.Write(html);
}
else
{
if (c == context.Session["checkdode"].ToString())//将请求的数据与存储的数据进行对比
{
if (name == "zhangsan" && password == "lisi")
{
string sql = "select * from cj";
DataTable dt = SqlHelper.ExecuteDataTable(sql, null);
for (int i = 0; i < dt.Rows.Count; i++)
{
context.Response.Write(dt.Rows[i]);
}
}
else
{
html = html.Replace("{meggtuon}", "密码错误");
context.Response.Write(html);
}
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
3.静态模板
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form action="dl.ashx" method="post">
<p> 请输入账号:<input type="text" name="name" /></p>
<p> 请输入密码:<input type="text" name="password" /></p>
<p>请输入验证码:<input type="text" name="checkdode" /><img src="/createcheckcode.ashx"/></p>
<p><input type="submit" name="dl" value="登录"/> {meggtuon}
</p>
</form>
</body>
</html>
asp.net 验证码(一)Session的更多相关文章
- 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中的Session怎么正确使用
Session对象用于存储从一个用户开始访问某个特定的aspx的页面起,到用户离开为止,特定的用户会话所需要的信息.用户在应用程序的页面切换时,Session对象的变量不会被清除. 对于一个Web应用 ...
- ASP.NET里的Session详细解释
Session模型简介 Session是什么呢?简单来说就是服务器给客户端的一个编号.当一台WWW服务器运行时,可能有若干个用户浏览正在运正在这台服务器上的网站.当每个用户首次与这台WWW服务器建立连 ...
- ASP.NET中的Session怎么正确使用[转]
Session对象用于存储从一个用户开始访问某个特定的aspx的页面起,到用户离开为止,特定的用户会话所需要的信息.用户在应用程序的页面切换时,Session对象的变量不会被清除. 对于一个Web应用 ...
- PHP+jQuery 注册模块的改进之一:验证码存入SESSION
/* ******* Date:2014-09-28 ******* Author:小dee ******* Blog:http://www.cnblogs.com/dee0912/*/ 对上一篇博文 ...
- ASP.NET会话(Session)保存模式--终于知道session为什么丢失了
[原创]ASP.NET会话(Session)保存模式 作者:寒羽枫(cityhunter172) 大家好,已有四个多月没写东东啦.今日抽空就说一下 Session 在 .Net v1.0/v1.1 中 ...
- 【转】asp.net中利用session对象传递、共享数据[session用法]
来自:http://blog.unvs.cn/archives/session-transfer-method.html 下面介绍Asp.net中利用session对象传递.共享数据用法: 1.传递值 ...
- 【译文】漫谈ASP.NET中的Session
最近这两天被一个Web Farm环境下的Session处理问题虐得很痛苦,网上到处找解决方案,在无意中翻看到这篇文章,感觉很不错,顺手查了一下,貌似没有现成的译文,于是一咬牙一跺脚把这篇文章翻译出来了 ...
随机推荐
- JS中定义对象原型的两种使用方法
第一种: function Person() { this.username = new Array(); this.password = "123"; } Person.prot ...
- Struts2中Date日期转换的问题
今天跑程序的时候莫名其妙的出现了下面的一个异常: java.lang.NoSuchMethodException:com.ca.agent.model.mybatis.ApprovalInforC ...
- 用flex做垂直居中
<div class="flex-cont flex-centerbox"> <div class="center-cont"> < ...
- LINQ
lambda表达式: LINQ to Object: 参考:http://www.cnblogs.com/leon-y-liu/articles/3575009.html LINQ to XML: u ...
- Lintcode 175. 翻转二叉树
-------------------- 递归那么好为什么不用递归啊...我才不会被你骗...(其实是因为用惯了递归啰嗦的循环反倒不会写了...o(╯□╰)o) AC代码: /** * Definit ...
- Dev GridView行拖拽
http://blog.csdn.net/keyrainie/article/details/8513802 http://www.cnblogs.com/qq4004229/archive/2012 ...
- LAMP_源码安装全教程
第一步:准备安装软件 httpd-2.4.7.tar.gz, apr-1.4.6.tar.gz, apr-util-1.4.1.tar.gz,mysql-5.5.tar.gz,php-5.4.tar. ...
- Visual Studio 禁用Resharpe插件
和普通的插件不一样,对于Resharper7+版本需要使用启动参数:/Resharper.Suspend
- 【转】TCP协议
TCP是什么? TCP(Transmission Control Protocol 传输控制协议)是一种面向连接(连接导向)的.可靠的. 基于IP的传输层协议.TCP在IP报文的协议号是6.TCP是一 ...
- js 怎么屏蔽微信打开网页后的分享
我们知道 js 可以通过 window.navigator.userAgent 来获取浏览器的相关信息,比如:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537. ...