zoukankan      html  css  js  c++  java
  • 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>

  • 相关阅读:
    nginx配置ssl验证
    腾讯云服务器、nginx部署loopback
    mongo删除指定字段,可多个字段同时删除
    前端axios下载excel无法获取header所有字段问题
    本机是wifi,虚拟机无法连接外网问题
    import文件时 ~/ 不识别问题(react)
    监听F5刷新,添加路由前缀
    Django学习笔记(13)model_to_dict 的使用
    Django学习笔记(12)基于前后端分离模式-添加用例接口实现
    Django学习笔记(11)url管理之include
  • 原文地址:https://www.cnblogs.com/liyiyong/p/5374645.html
Copyright © 2011-2022 走看看