评论

收藏

[R语言] asp.net代码练习 work068 演示随机生成登录验证码

编程语言 编程语言 发布于:2021-12-04 23:17 | 阅读数:393 | 评论:0

webform1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="work068.WebForm1" %>
<!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>
  
  </div>
  </form>
</body>
</html>
webform1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace work068
{
  public partial class WebForm1 : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      //演示随机生成登录验证码
      //验证码的字符集合
      string checkCodeString = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
      //集合的长度
      int length = checkCodeString.Length;
      //建立字体,粗体
      System.Drawing.Font font1 = new System.Drawing.Font("宋体", 24, System.Drawing.FontStyle.Bold);
      //预备空画刷
      System.Drawing.Brush brush1 = null;
      //预备画刷颜色
      System.Drawing.Color brushColor = new System.Drawing.Color();
      //保存最后生成的验证码字符串
      string checkCode = string.Empty;
      //单个验证码
      string code = string.Empty;
      //建立位图
      System.Drawing.Bitmap image1 = new System.Drawing.Bitmap(100, 40);
      //从位图获取图像对象
      System.Drawing.Graphics graphic1 = System.Drawing.Graphics.FromImage(image1);
      //背景白色
      graphic1.Clear(System.Drawing.Color.White);
      //随机数
      Random random1 = new Random();
      //4位
      for (int i = 0; i < 4; i++)
      {
        //取模运算
        int current = random1.Next(DateTime.Now.Millisecond) % length;
        //验证码字符串集合中截取字符
        code = checkCodeString.Substring(current, 1);
        //累加至验证码字符串中
        checkCode += code;
        //随机生成颜色
        brushColor = System.Drawing.Color.FromArgb(random1.Next(255), random1.Next(255), random1.Next(255));
        //给画刷加颜色
        brush1 = new System.Drawing.SolidBrush(brushColor);
        //图像对象中写入字符串,字符起点20像素宽,中间5像素隔开
        graphic1.DrawString(code, font1, brush1, i * 20 + 5, 10);
      }
      //网页清空
      Response.Clear();
      //设置网页模式
      Response.ContentType = "image/pjpeg";
      //图片保存至网页流中
      image1.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
      //最后的验证码保存至会话层中
      Session["CheckCode"] = checkCode;
      //释放资源
      graphic1.Dispose();
      image1.Dispose();
      Response.End();
    }
  }
}
webform2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="work068.WebForm2" %>
<!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>
    <table border="0">
      <tr>
        <td>用户名</td>
        <td>
          <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
          <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="必填" ControlToValidate="txtUserName" Display="Dynamic"></asp:RequiredFieldValidator>
        </td>
      </tr>
      <tr>
        <td>密码</td>
        <td>
          <asp:TextBox ID="txtPwd" runat="server"></asp:TextBox>
          <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="必填" ControlToValidate="txtPwd" Display="Dynamic"></asp:RequiredFieldValidator>
        </td>
      </tr>
      <tr>
        <td>验证码</td>
        <td>
          <asp:TextBox ID="txtCheckCode" runat="server"></asp:TextBox>
          <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="必填" ControlToValidate="txtCheckCode" Display="Dynamic"></asp:RequiredFieldValidator>
          <img src="WebForm1.aspx" width="100" height="40" style="cursor:pointer"  alt="点击刷新验证码"/>
        </td>
      </tr>
      <tr>
        <td colspan="2">
          <asp:Button ID="Button1" runat="server" Text="登录"  />
        </td>
      </tr>
    </table>
  </div>
  </form>
</body>
</html>
webform2.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace work068
{
  public partial class WebForm2 : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      Page.UnobtrusiveValidationMode = System.Web.UI.UnobtrusiveValidationMode.None;
      txtUserName.Text = "虾米大王";
      txtPwd.Text = "123";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
      string checkCode = Session["CheckCode"].ToString();
      if (checkCode.ToUpper() == txtCheckCode.Text.ToUpper())
      {
        Response.Write("<script>window.alert('验证码正确,登录成功');</script>");
      }
      else
      {
        Response.Write("<script>window.alert('验证码错误,重新登录');</script>");
      }
    }
  }
}

关注下面的标签,发现更多相似文章