应用程序的第九个事件:PostAcquireRequestState 获取Session (先判断当前页面对象是否实现了IRequiresSessionState接口,如果实现了,则从浏览器发来的请求报文中获得SessionId,并到服务器的Session池中获得对于的Session对象。最后赋值给HttpContext的Session属性)
注意:在一般处理程序中不能直接使用Session。需要继承IRequiresSessionState接口才可以使用Session
IRequiresSessionState是一个标记接口,没有实际用处。在一般处理程序中,为了提高性能,在asp.neet应用程序生命周期中,会判断ValidCodeashx这个类型是否有实现这个IRequiresSessionState接口,如果有实现这个接口,,就证明将来你可能需要用到这个session,这样才会走创建Session这些操作。如果没有实现这不走创建session这个操作。
一般处理程序:ValidCodeashx.ashx 这个一般处理程序负责生成一张图片
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.SessionState;
namespace WebApp
{
/// <summary>
/// 注意:在一般处理程序中不能直接使用Session。需要继承IRequiresSessionState接口才可以使用Session
/// IRequiresSessionState是一个标记接口,没有实际用处。在一般处理程序中,为了提高性能,在asp.neet应用程序生命周期中,会判断ValidCodeashx这个类型是否有实现这个IRequiresSessionState接口,如果有实现这个接口,,就证明将来你可能需要用到这个session,这样才会走创建Session这些操作。如果没有实现这不走创建session这个操作。
/// </summary>
public class ValidCodeashx : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
//1.随即生成4位数字(及1000到9999之间的数字)
Random rdm = new Random();
int number = rdm.Next(1000, 10000);
context.Session["ValidCode"] = number.ToString();
CreateValidateGraphic(number.ToString(), context);
}
/// <summary>
/// 创建验证码的图片
/// </summary>
/// <param name="containsPage">要输出到的page对象</param>
/// <param name="validateNum">验证码</param>
public void CreateValidateGraphic(string validateCode, HttpContext context)
{
Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * 12.0), 22);
Graphics g = Graphics.FromImage(image);
try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的干扰线
for (int 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 Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),
Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(validateCode, font, brush, 3, 2);
//画图片的前景干扰点
for (int i = 0; i < 100; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
//保存图片数据
MemoryStream stream = new MemoryStream();
image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
//输出图片流
context.Response.Clear();
context.Response.ContentType = "image/jpeg";//设置报文头
context.Response.BinaryWrite(stream.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
WebForm 应用程序:在这里调用一般处理程序ValidCodeashx.ashx
WebForm4.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="WebApp.WebForm4" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:Image ID="Image1" runat="server" ImageUrl="~/ValidCodeashx.ashx" /><asp:Button ID="Button1" runat="server" Text="提交" OnClick="Button1_Click" /><label ><%=Message %></label>
</div>
</form>
</body>
</html>
WebForm4.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApp
{
public partial class WebForm4 : System.Web.UI.Page
{
protected string Message;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string vcode=Session["ValidCode"].ToString();
if (this.TextBox1.Text != vcode)
{
Message = "验证码不正确";
}
else
{
Message = "验证码正确";
}
}
}
}
输入4150后点提交
------------
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="检查登陆验证.Login" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="jquery-1.11.2.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="用户名"></asp:Label><asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Text="密码"></asp:Label><asp:TextBox ID="txtPassWord" runat="server" TextMode="Password"></asp:TextBox>
<asp:Image ID="Image1" runat="server" ImageUrl="~/ashx/ValidCodeashx.ashx" />
</div>
</form>
</body>
</html>
<script type="text/javascript">
$(function () {
$(<%=this.Image1.ClientID %>).click(function () {
$(this).attr("src", "/ashx/ValidCodeashx.ashx?"+ new Date().getMilliseconds());
})
})
</script>