验证码的创建与使用方法,结合实例形式较为详细的分析了c#验证码的创建、验证等操作步骤与相关技巧,需要的朋友可以参考下本文实例讲述了C#验证码的创建与使用方法。分享给大家供大家参考,具体如下:1、C#创建验证码① 创建获取验证码页面(ValidateCode.asp">

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

来源:这里教程网 时间:2026-02-21 13:41:44 作者:

这篇文章主要介绍了c#http://www.php.cn/php/php-tp-codes.html" target="_blank">验证码的创建与使用方法,结合实例形式较为详细的分析了c#验证码的创建、验证等操作步骤与相关技巧,需要的朋友可以参考下

本文实例讲述了C#验证码的创建与使用方法。分享给大家供大家参考,具体如下:

1、C#创建验证码

① 创建获取验证码页面(ValidateCode.aspx)

<html xmlns="https://www.herecours.com/d/file/efpub/2026/21-21/20260221130914169048.jpg);
    Response.ClearContent();
    Response.ContentType = "image/Gif";
    Response.BinaryWrite(ms.ToArray());
  }
  finally
  {
    g.Dispose();
    image.Dispose();
  }
}

2、验证码的使用

① 验证码的前段显示代码

 代码如下:

@@##@@

② 创建验证码测试页面(ValidateTest.aspx)

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>验证码测试</title>
</head>
<body>
  <form id="form1" runat="server">
  <p>
    <input runat="server" id="txtValidate" />
    @@##@@
    <asp:Button runat="server" id="btnVal" Text="提交" onclick="btnVal_Click" />
  </p>
  </form>
</body>
</html>

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

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#验证码的创建与使用的示例代码分享C#验证码的创建与使用的示例代码分享

相关推荐