|
Written by Praveen V Nair
|
|
Apr 30, 2007 at 04:17 AM |
|
Author: PraVeeN - Microsoft MVP
Confirmation Code Generator with GDI+ for ASP.NET 2.0
Some years back I did a small program for generating confirmation numbers for
websites with PHP (http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1289&lngWId=8).
This script will help the webmasters to block hackers which use automated
programs.
This is an ASP.NET version of the same program.
Here 'n' is the number of characters to display. You can take Session["strconfirm"] for validations in your
program.
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;
#region About
// Purpose: Confirmation code generator for /hackers/
// Date: 20 Dec 2006
// Author: Praveen
#endregion
public partial class pitConfirm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int n = 5;
if (Request["n"] != null)
{
try
{
n = int.Parse(Request["n"]);
}
catch {
// do nothing ;)
}
}
DrawConfirm(n);
}
private void DrawConfirm(int n)
{
Random r = new Random();
string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int y = 37, mx = 20, x = mx*n;
Bitmap b = new Bitmap(x, y);
string strconfirm = string.Empty;
for (int i = 0, x1 = 0; i < n; i++, x1+=mx)
{
string str = chars.Substring(r.Next(0, 36), 1);
strconfirm += str;
Bitmap b2 = (Bitmap)Bitmap.FromFile(Server.MapPath(@"images\confirm_images\"+ str +".gif"));
Graphics g = Graphics.FromImage(b);
g.DrawImage(b2, x1, 0);
}
b.Save(Response.OutputStream, ImageFormat.Gif);
Session["strconfirm"] = strconfirm;
}
}
|