asp.net登录时生成验证码的方法

在CommonClass.cs中定义RandomNum(4)的方法:

    /// <summary>
    /// 实现随机验证码
    /// </summary>
    /// <param name="n">验证码个数</param>
    /// <returns>返回生成的随机数</returns>
    public string RandomNum(int n) {
        string strchar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
        string[] VcArray = strchar.Split(',');
        string VNum = "";
        int temp = -1;
        Random rand=new Random();
        for (int i=1; i < n + 1; i++) {
            if (temp != -1) {
                rand = new Random(i*temp*unchecked((int)DateTime.Now.Ticks));
            }
            int t = rand.Next(61);
            if (temp != -1 && temp == t) {
                return RandomNum(n);
            
            }
            temp = t;
            VNum += VcArray[t];
        }
        return VNum;
    
    
    }

.aspx代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
    .tbl_login{ padding:10px; margin:10px;}
    .tbl_login td{ padding:10px; border:1px solid gray;}
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Table ID="Table1" runat="server" CssClass="tbl_login">
            <asp:TableRow runat="server">
            <asp:TableCell>管理员姓名:</asp:TableCell>
            <asp:TableCell><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></asp:TableCell>
            </asp:TableRow>
            <asp:TableRow runat="server">
             <asp:TableCell>管理员密码:</asp:TableCell>
            <asp:TableCell><asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox></asp:TableCell>
            </asp:TableRow>
            <asp:TableRow ID="TableRow1" runat="server">
             <asp:TableCell>验证码:</asp:TableCell>
            <asp:TableCell><asp:TextBox ID="TextBox3" runat="server" ></asp:TextBox></asp:TableCell>
            <asp:TableCell style="border:1px solid silver;">
                <asp:Label ID="lab_Code" runat="server" Text="8888"></asp:Label></asp:TableCell>
            </asp:TableRow>
            <asp:TableRow runat="server">
            <asp:TableCell>
                <asp:Button ID="btn_login" runat="server" Text="登录" /></asp:TableCell>
             <asp:TableCell>
                 <asp:Button ID="btn_cancel" runat="server" Text="取消" /></asp:TableCell>
            </asp:TableRow>
           
        </asp:Table>
    </div>
    </form>
</body>
</html>


.aspx.cs代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Login : System.Web.UI.Page
{
    CommonClass cc = new CommonClass();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) {
            this.lab_Code.Text = cc.RandomNum(4);
        
        }
    }

}

效果如下:


  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是ASP.NET验证码代码: ASPX页面: ```html <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Captcha.aspx.cs" Inherits="Captcha" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Image ID="imgCaptcha" runat="server" /> <br /> <asp:TextBox ID="txtCaptcha" runat="server"></asp:TextBox> <asp:Button ID="btnCheck" runat="server" Text="Check" OnClick="btnCheck_Click" /> </div> </form> </body> </html> ``` ASPX.CS页面: ```csharp using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Web.UI; public partial class Captcha : Page { private string GenerateRandomCode(int length) { const string chars = "ABCDEFGHJKMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz123456789"; var random = new Random(); var result = new char[length]; for (var i = 0; i < length; i++) { result[i] = chars[random.Next(chars.Length)]; } return new string(result); } protected void Page_Load(object sender, EventArgs e) { var randomCode = GenerateRandomCode(6); Session["Captcha"] = randomCode; using (var bitmap = new Bitmap(100, 30)) { using (var graphics = Graphics.FromImage(bitmap)) { graphics.Clear(Color.White); graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.DrawString(randomCode, new Font("Arial", 16), Brushes.Black, new Point(10, 0)); Response.ContentType = "image/png"; bitmap.Save(Response.OutputStream, ImageFormat.Png); } } } protected void btnCheck_Click(object sender, EventArgs e) { if (txtCaptcha.Text.Equals(Session["Captcha"])) { Response.Write("Correct!"); } else { Response.Write("Incorrect!"); } } } ``` 这个代码生成一个6位随机验证码,并将其存储在会话中。然后使用Graphics类在ASP.NET页面上绘制验证码图像,并将其发送回客户端。用户输入他们看到的验证码并单击“检查”按钮, ASP.NET代码将会检查用户输入是否与会话中存储的验证码匹配。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值