打印

[asp.net教程] asp.net随机码生成示例

asp.net随机码生成示例

现在很多网页登陆的时候都使用了随机图片的方式,是一种简单、有效的防止黑客恶意攻击的手段。今天看了一些网上的资料,明白其生成原理:从样本中,获取随机字符串,随机字符串保存进session,并以位图的方式形成随机码图片。
   
  实现:
  添加命名空间
   
  using system.drawing;
  using system.drawing.imaging;
  using system.drawing.drawing2d;
  生成页代码
   
  using system;
  using system.data;
  using system.configuration;
  using system.collections;
  using system.web;
  using system.web.security;
  using system.web.ui;
  using system.web.ui.webcontrols;
  using system.web.ui.webcontrols.webparts;
  using system.web.ui.htmlcontrols;
  using system.drawing;
  using system.drawing.imaging;
  using system.drawing.drawing2d;
   
  public partial class getrandimg : system.web.ui.page
  {
   protected void page_load(object sender, eventargs e)
   {
   //生成随机码图片
   setvalidatecode();
   //生成页面不保存到cache

   response.cache.setnostore();
   }
   
   //设置验证码
   private void setvalidatecode()
   {
   //新建位图
   bitmap newbitmap = new bitmap(
   71,
   23,
   pixelformat.format32bppargb
   );
   //从位图获得绘图画面
   graphics g = graphics.fromimage(newbitmap);
   //随机数生成器
   random r = new random();
   //绘图画面清空
   g.clear(color.white);
   //绘图画面划线干扰
   for (int i = 0; i < 50; i++)
   {
   int x1 = r.next(newbitmap.width);
   int x2 = r.next(newbitmap.width);
   int y1 = r.next(newbitmap.height);
   int y2 = r.next(newbitmap.height);
   g.drawline(new pen(
   color.fromargb(r.next())),
   x1,
   y1,
   x2,
   y2
   );
   }
   //绘图画面点数干扰
   for (int i = 0; i < 100; i++)
   {
   int x = r.next(newbitmap.width);
   int y = r.next(newbitmap.height);
   newbitmap.setpixel(
   x,
   y,
   color.fromargb(r.next())
   );
   }
   //获得随机字符串(5位长度)
   string value = generaterandom(5);
   //随机字符串赋值给session
   session["randcode"] = value;
   //定义图片显示字体样式
   font font = new font(
   "arial",
   14,
   fontstyle.bold
   );
   random rr = new random();
   int yy = rr.next(1, 4);
   //定义随机字符串显示图片刷子
   lineargradientbrush brush = new lineargradientbrush(
   new rectangle(0, 0, 71, 23),
   color.red,
   color.blue,
   1.2f,
   true
   );
   g.drawstring(value, font, brush, 2, yy);
   g.drawrectangle(new pen(
   color.silver),
   0,
   0,
   70,
   22
   );
   system.io.memorystream ms = new system.io.memorystream();
   newbitmap.save(ms, imageformat.gif);
   //输出图片
   response.clearcontent();
   response.contenttype = "image/gif";
   response.binarywrite(ms.toarray());
   }
   
   //常量集
   private static char[] constant ={
   '0','1','2','3','4','5','6','7','8','9',
   'a','b','c','d','e','f','g','h','i','j',
   'k','l','m','n','o','p','q','r','s','t',
   'u','v','w','x','y','z'
   };
   
   
   //生成随机字符串
   public static string generaterandom(int length)
   {
   system.text.stringbuilder newrandom = new system.text.stringbuilder(36);
   random rd = new random();
   for (int i = 0; i < length; i++)
   {
   newrandom.append(constant[rd.next(36)]);
   }
   return newrandom.tostring();
   }
  }
   
  使用随机图片的页面,image控件的写法如下:
   
  <asp:image id="image1" imageurl="~/getrandimg.aspx" runat="server" />
   
  示例代码:http://www.cnblogs.com/files/heekui/randcode.rar
   
  http://www.cnblogs.com/heekui/archive/2007/01/06/613609.html

TOP

返回顶部
AYBlue

Processed in 0.059233 second(s), 7 queries.

当前时区 GMT+8, 现在时间是 2009-1-10 11:57 京ICP备06054220号

清除 Cookies - 联系我们 - 163K.com - Archiver - WAP