您的位置:首页 > 其它

验证码| 在一般处理程序中使用Session

2015-09-16 18:55 393 查看
注意:在一般处理程序中不能直接使用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后点提交

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: