Inside W3Hearts
Categories
Windows Programming
C#
Visual Basic 6.0 / .NET
C / C++ / Visual C++ (MFC , SDK)
Packaging & Deployment
Web Programming
ASP.NET
ASP.NET Webservices
PHP & MySQL
J2EE / Servlet / Struts
Client side coding
Javascript & AJAX
CSS
HTML / XHTML
Database
SQL Server
Oracle
MySQL
 
 Web Programming >> ASP.NET  
 
Use of Image Control to Prevent Automated Website Registrations Using Asp.NET
by Shashidhar P T
Jun 30th 08

Nowadays in all the site it is general that, to become a member user has to enter text that is displayed in an image after filling his general information. The image with text displayed is randomly generated for each registration. This is to make sure that user has created his own profile. This prevents automated generation to site.

Simple Way of Integrating Autogenerated Image with Text

Major Steps Involved to prevent automated website registrations

1. Generate a random string
2. Generate an image with the random string that was generated in the earlier step

Code to generate random string using inbuilt Random class

Code

public static string GenerateString()
{
int iSLength=0, iRN=0;
string sRString;
Random rR =new Random(System.DateTime.Now.Millisecond);
sRString = "";
while (iSLength < 5){
iRN = rR.Next(0, 86);
if(((iRN >= 0) && (iRN<= 9) || (iRN >= 65) && (iRN <= 86))){
sRString = sRString + (char)iRN;
iSLength = iSLength + 1;
}
}
return sRString;
}
 

 
In the above code you are generating a random number and that random number is converted to a character if the number is equal to or between 0 and 9, or between 65 and 86. You may notice that the number 65 corresponds to letter ‘A’ and you can calculate the number for character ‘Z’. The characters are concatenated to string variable until you reach the length of the string, in this sample length of the string is 4. The code for generating a string can be within a function so that it can be used while generating the image. 
Code to generate image with string
Code
Bitmap bmpImage=new Bitmap(80,25);
Graphics g=Graphics.FromImage(bmpImage);
g.Clear(Color.Red);
string sRS=GenerateString();
Session["sessionRS"]=sRS;
g.DrawString(sRS,new Font("Arial",14),new SolidBrush(Color.Black),2,2);
bmpImage.Save(Response.OutputStream,ImageFormat.Gif);
bmpImage.Dispose();
 
In the above code you can see GenerateString() method is called while the geneartion of the image and is outputted to response stream. This output is fed to the Image control that is used in the registration form.
Code to call the Image Control in registration form
Code
<asp:Image id=Image1 runat="server" ImageUrl="url-of-the-page-that-generates-image" />
 
url-of-the-page-that-generates-image : The form that generates the image with string is the URL of the Image Control in the registration form. This enables the generated image to be bound to the image control for display in the form.
To compare the string against keyed in by the user

If you observe the code of generating image with string, string is stored in session variable prior to creation of image. You can make use of the string stored in session variable to compare the string keyed in by user in form and display the error message if necessary. The above code for generating the image is placed within an “if” block which checks whether the user accesses the page for the first time. If the user is accessing the page for the first time then the image is generated, otherwise not. 

 
 
 

Discussions

Need to discuss about this article ? Click here
 
© 2007 W3hearts.com
Home | Post Article | Start discussion | Join Community| Privacy policy