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();
}
本文介绍了一个简单的验证码生成及验证逻辑实现。通过随机数生成六位数验证码,并设置20秒的有效期。用户输入验证码后,系统会检查验证码的有效性和正确性。
980

被折叠的 条评论
为什么被折叠?



