asp.net代码练习 work068 演示随机生成登录验证码

258 篇文章 2 订阅

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" onclick="javascript:this.src='WebForm1.aspx?id=' + Math.random() * 10000 " alt="点击刷新验证码"/>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Button ID="Button1" runat="server" Text="登录" OnClick="Button1_Click" />
                </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>");
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虾米大王

有你的支持,我会更有动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值