asp.net登入時產生驗證碼的方法

來源:互聯網
上載者:User

標籤: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);                }    }}

效果如下:


asp.net登入時產生驗證碼的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.