正文

C#验证码的创建与使用示例

︶ㄣ米米素材网 メ

③ 编写验证码测试的提交代码(ValidateTest.aspx.cs)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
protected void btnVal_Click(object sender, EventArgs e)
{
  bool result = false//验证结果
  string userCode = this.txtValidate.Value; //获取用户输入的验证码
  if (String.IsNullOrEmpty(userCode))
  {
    //请输入验证码
    return;
  }
  string validCode = this.Session["CheckCode"] as String; //获取系统生成的验证码
  if (!string.IsNullOrEmpty(validCode))
  {
    if (userCode.ToLower() == validCode.ToLower())
    {
      //验证成功
      result = true;
    }
    else
    {
      //验证失败
      result = false;
    }
  }
}

希望本文所述对大家C#程序设计有所帮助。