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);
        
        }
    }

}

效果如下:


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。