winform随机验证码生成和验证

来源:互联网 发布:mac invalid argument 编辑:程序博客网 时间:2024/04/19 16:54

 int CurrentCheckNo = 0;
        DateTime CheckNoTime = DateTime.Now;

 

              private void AutoCheckNO()     //验证码的生成
        {
            Random rnd = new Random();
            CurrentCheckNo = rnd.Next(100000, 999999);
            CheckNoTime = DateTime.Now;
            this.CheckNo.Text = CurrentCheckNo.ToString();
        }

        private void checkno_Click(object sender, EventArgs e)
        {

            AutoCheckNO();

        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (CheckNoTime.AddSeconds(20) <= DateTime.Now)//判断验证码是否超时
            {
                MessageBox.Show("验证码无效!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                AutoCheckNO();
                return;
            }

            string ChkNO = edtCheckNo.Text;

            if (!this.CurrentCheckNo.ToString().Equals(ChkNO))
            {
                MessageBox.Show("验证码错误!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                AutoCheckNO();
                return;
            }

            Program.isLogin = true;
            this.Close();

        }

原创粉丝点击