国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > .NET > 正文

用C#(asp.net)寫(xiě)出登錄驗(yàn)證碼

2024-07-10 13:00:05
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
public class validatecode : system.web.ui.page
 {
  private void page_load(object sender, system.eventargs e)
  {
   this.createcheckcodeimage(generatecheckcode());
  }

  #region web 窗體設(shè)計(jì)器生成的代碼
  override protected void oninit(eventargs e)
  {
   //
   // codegen: 該調(diào)用是 asp.net web 窗體設(shè)計(jì)器所必需的。
   //
   initializecomponent();
   base.oninit(e);
  }
 
  /// <summary>
  /// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內(nèi)容。
  /// </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)
  {
   if(checkcode == null || checkcode.trim() == string.empty)
    return;

   system.drawing.bitmap image = new system.drawing.bitmap((int)math.ceiling((checkcode.length * 12.5)), 22);
   graphics g = graphics.fromimage(image);

   try
   {
    //生成隨機(jī)生成器
    random random = new random();

    //清空?qǐng)D片背景色
    g.clear(color.white);

    //畫(huà)圖片的背景噪音線
    for(int i=0; i<25; i++)
    {
     int x1 = random.next(image.width);
     int x2 = random.next(image.width);
     int y1 = random.next(image.height);
     int y2 = random.next(image.height);

     g.drawline(new pen(color.silver), x1, y1, x2, y2);
    }

    font font = new system.drawing.font("arial", 12, (system.drawing.fontstyle.bold | system.drawing.fontstyle.italic));
    system.drawing.drawing2d.lineargradientbrush brush = new system.drawing.drawing2d.lineargradientbrush(new rectangle(0, 0, image.width, image.height), color.blue, color.darkred, 1.2f, true);
    g.drawstring(checkcode, font, brush, 2, 2);

    //畫(huà)圖片的前景噪音點(diǎn)
    for(int i=0; i<100; i++)
    {
     int x = random.next(image.width);
     int y = random.next(image.height);

     image.setpixel(x, y, color.fromargb(random.next()));
    }

    //畫(huà)圖片的邊框線
    g.drawrectangle(new pen(color.silver), 0, 0, image.width - 1, image.height - 1);

    system.io.memorystream ms = new system.io.memorystream();
    image.save(ms, system.drawing.imaging.imageformat.gif);
    response.clearcontent();
    response.contenttype = "image/gif";
    response.binarywrite(ms.toarray());
   }
   finally
   {
    g.dispose();
    image.dispose();
   }
  }
 }

  假如以上驗(yàn)證碼生成器頁(yè)面名為:checkcode.aspx,那么在登錄頁(yè)面中使用“<img>” 這個(gè) html 元素來(lái)顯示生成的驗(yàn)證碼圖片:<img src="checkcode.aspx">
  在登錄頁(yè)面的登錄按鈕的處理事件中使用以下代碼判斷驗(yàn)證碼:


  private void btnlogin_click(object sender, system.web.ui.imageclickeventargs e)
  {
   if(request.cookies["checkcode"] == null)
   {
    lblmessage.text = "您的瀏覽器設(shè)置已被禁用 cookies,您必須設(shè)置瀏覽器允許使用 cookies 選項(xiàng)后才能使用本系統(tǒng)。";
    lblmessage.visible = true;
    return;
   }

   if(string.compare(request.cookies["checkcode"].value, txtcheckcode.text, true) != 0)
   {
    lblmessage.text = "驗(yàn)證碼錯(cuò)誤,請(qǐng)輸入正確的驗(yàn)證碼。";
    lblmessage.visible = true;
    return;
   }

/******其他代碼******/
  }

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 兰溪市| 富平县| 屯留县| 油尖旺区| 商南县| 万盛区| 安达市| 大姚县| 扎兰屯市| 广水市| 昭通市| 台中县| 陈巴尔虎旗| 卫辉市| 榆林市| 阿拉尔市| 岫岩| 曲麻莱县| 大城县| 宽甸| 大渡口区| 东方市| 永泰县| 泽州县| 太保市| 文登市| 平顺县| 镇平县| 龙井市| 枣强县| 眉山市| 义乌市| 桑植县| 靖宇县| 宁明县| 伽师县| 迭部县| 南郑县| 凭祥市| 肥西县| 和平区|