验证码

Login.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>登录</title>
    <script type="text/javascript">
        function RefreshCode()
        {
            var CheckCode = document.getElementById("CheckCode");
            CheckCode.src = "CheckCode.ashx?t="+new Date();
        }
    </script>
</head>
<body>
    <form action="Login.ashx" method="post">
        姓名:<input type="text" name="txtName" />
        密码:<input type="password" name="txtPwd" />
        验证码:<img src="CheckCode.ashx" id="CheckCode" onclick="RefreshCode()" />
        <input type="text" name="txtCode" />
        <input type="submit" name="btnLogin" value="提交" />
        <div style="padding:10px; color:red;">{^msg$}</div>
    </form>
</body>
</html>

  

Login.ashx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
namespace _4_3SessionCase
{
    /// <summary>
    /// Login 的摘要说明
    /// </summary>
    public class Login : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            string html = CommonHelper.ReadHtml("~/Login.html");
            string btnLogin = context.Request["btnLogin"];
            if (string.IsNullOrEmpty(btnLogin))
            {
                //加载页面
                html = html.Replace("{^msg$}", "");
                context.Response.Write(html);
            }
            else
            {
                //提交表单
                string userName = context.Request["txtName"];
                string pwd = context.Request["txtPwd"];
                string checkCode = context.Request["txtCode"];
                string SessionCheckCode = (string)context.Session[LoginSession.CHECKCODE];
                if (checkCode!=SessionCheckCode)
                {
                    //验证码检测
                    html = html.Replace("{^msg$}", "错误提示:验证码错误");
                    context.Response.Write(html);
                    return;
                }

                if (userName != "admin" && pwd != "123")
                {
                    //用户名密码检测
                    html = html.Replace("{^msg$}", "错误提示:用户名或密码错误");
                    context.Response.Write(html);
                    return;
                }

                context.Session[LoginSession.USERNAME] = userName;
                context.Session.Timeout = 20;
                string url = (string)context.Session[LoginSession.LOGINURL];
                if (string.IsNullOrEmpty(url))
                {
                    //默认跳转
                    context.Response.Redirect("ChangePwd.ashx");
                }
                else
                {
                    //跳转到进入页面
                    context.Response.Redirect(url);
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

  

CheckCode.ashx

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace _4_3SessionCase
{
    /// <summary>
    /// CheckCode 的摘要说明
    /// </summary>
    public class CheckCode : IHttpHandler,IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            //动态生成一张200*50的图片,显示一个随机的4位数。
            context.Response.ContentType = "image/jpeg";
            using (Bitmap bmp = new Bitmap(80, 30))//创建一个尺寸为500*500的内存图片
            using (Graphics g = Graphics.FromImage(bmp))//得到图片的画布
            using (Font font = new Font(FontFamily.GenericSerif, 20))
            {
                Random rd = new Random();
                int num = rd.Next(1000,10000);
                context.Session[LoginSession.CHECKCODE]=num.ToString();
                HttpRequest request = context.Request;
                g.DrawString(num.ToString(), font, Brushes.Red, 10, 0);
                bmp.Save(context.Response.OutputStream, ImageFormat.Jpeg);//图片保存到输出流
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

  

 

posted @ 2015-06-15 09:55  linyongqin  阅读(199)  评论(0)    收藏  举报