您的位置:首页 > 编程语言 > ASP

asp.net 用户登录 验证码

2017-07-09 21:19 309 查看
using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!Page.IsPostBack)//判断页面是不是第一次显示

        {

            labCode.Text = RandomNum(4);

        }

    }

    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";

        //将strchar 字符串转化为数组

        //String.Split 方法放回包含此实例中的子字符串(由指定Char数组的元素分割)的String数组

        string[] VcArray = strchar.Split(',');

        string VNum = "";

        //记录上次随机数组,避免产生几个一样的

        int temp = -1;

        //采用一个简单的算法以保证 生产随机数的不同

        Random rand = new Random();

        for (int i = 1; i < n + 1; i++)

        {

            if (temp != -1)

            {

                //unchecked 关键字用于取消整型算术运算和转换的溢出检查。

                //DataTime.Ticks 属性获取表示此实例的日期和时间的刻度数。

                rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));

            }

            //Random.Next 方法返回一个小于所指定最大值的非负数随机数。

            int t = rand.Next(61);

            if (temp != -1 && temp == t)

            {

                return RandomNum(n);

            }

            temp = t;

            VNum += VcArray[t];

        }

        return VNum;//返回生成的随机数

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        if(this.TextBox1.Text.Trim() =="" || this.TextBox2.Text.Trim() =="")

        {

            this.ClientScript.RegisterStartupScript

                (this.GetType(), "", "<script>alert('用户名和密码不能为空')</script>");

            return;

        }

        if (this.TextBox3.Text != labCode.Text)

        {

            this.ClientScript.RegisterStartupScript

                (this.GetType(), "", "<script>alert('验证码不正确')</script>");

            return;

        }

        if (TextBox1.Text == "admin" && TextBox1.Text == "admin")

        {

            Response.Redirect("Creat.aspx?userName=" + TextBox1.Text + "");

        }

        else

        {

            this.ClientScript.RegisterStartupScript

               (this.GetType(), "", "<script>alert('用户名或密码不正确')</script>");

        }

    }

    protected void Button2_Click(object sender, EventArgs e)

    {

        labCode.Text = RandomNum(4);

    }
}

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

public partial class Creat : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!Page.IsPostBack)

        {

            if (Request.QueryString["userName"] != null)

            {

                this.Label1.Text =

                    "    欢迎登陆" + Request.QueryString["userName"] + "<br/>";

                this.Label1.Text +=

                    "    当前时间是:  " + DateTime.Now.ToString();

            }

            else

            {

                Response.Redirect("Default.aspx");

            }

                

                

            }

        }

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

public partial class Creat : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!Page.IsPostBack)

        {

            if (Request.QueryString["userName"] != null)

            {

                this.Label1.Text =

                    "    欢迎登陆" + Request.QueryString["userName"] + "<br/>";

                this.Label1.Text +=

                    "    当前时间是:  " + DateTime.Now.ToString();

            }

            else

            {

                Response.Redirect("Default.aspx");

            }

                

                

            }

        }

    }

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: