asp.net 用户登录 验证码


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 =
                    "&nbsp; &nbsp; 欢迎登陆" + Request.QueryString["userName"] + "<br/>";
                this.Label1.Text +=
                    "&nbsp; &nbsp; 当前时间是:  " + 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 =
                    "&nbsp; &nbsp; 欢迎登陆" + Request.QueryString["userName"] + "<br/>";
                this.Label1.Text +=
                    "&nbsp; &nbsp; 当前时间是:  " + DateTime.Now.ToString();
            }
            else
            {
                Response.Redirect("Default.aspx");
            }
                
                


            }
        }


    }

    }


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值