放大镜站内搜索:


您现在的位置:首页 > 电脑 > 应用技巧 > 正文
ASP.net 验证码(C#)
学生王国 ∥ http://www.xswg.com 更新时间:2006年5月1日 调整字体:

/* Copyright all(c) 2005 ZhongFeng, http://blog.csdn.net/SW515 */
 public class ValidateCode : System.Web.UI.Page
 {
  private void Page_Load(object sender, System.EventArgs e)
  {
   this.CreateCheckCodeImage(GenerateCheckCode());
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //

   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>

  private void InitializeComponent()
  {   
   this.Load += new System.EventHandler(this.Page_Load);
  }
  #endregion

  private string GenerateCheckCode()
  {
   int number;
   char code;
   string checkCode = String.Empty;

   System.Random random = new Random();

   for(int i=0; i<5; i++)
   {
    number = random.Next();

    if(number % 2 == 0)
     code = (char)('0' + (char)(number % 10));
    else
     code = (char)('A' + (char)(number % 26));

    checkCode += code.ToString();
   }

   Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));

   return checkCode;
  }

  private void CreateCheckCodeImage(string checkCode)
  {

3共  1 2 3 下一页


相关链接
来自:SW515博客
编辑:lgh
浏览:
评论查看全部评论
您的评论
姓名:
请您注意:
1.请遵守《中国互联网行业自律条款》及中华人民共和国其他各项有关法律法规。
2.严禁发表危害国家安全、损害国家利益、破坏民族团结、破坏国家宗教政策、破坏社会稳定、侮辱、诽谤、教唆、淫秽等内容的评论 。
3.用户需对自己在使用本站服务过程中的行为承担法律责任(直接或间接导致的)。
4.本站管理员有权保留或删除评论内容。
评论内容只代表网友个人观点,与本网站立场无关。