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>

转载于:https://www.cnblogs.com/liyiyong/p/5374645.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值