asp.net dynamic generation of verification code

Source: Internet
Author: User
Tags httpcontext rand
Asp.net| News | Verification Code

We are in the design of user login module, often use the verification code, can effectively prevent hacker software malicious crack, now exposes my commonly used authentication code source code.

How to use:

1, in the Web project to add a class, such as "CreateImage.cs", and then I published the source code copy in;

2, and then create a new Web form, such as "image.aspx", add code in Page_Load "Createimage.drawimage ();" Of course don't forget to add a reference to the class Oh!!

3, in the appropriate location of the page (you want to put the location of the verification code) add the following JavaScript code on the OK,

<script language= "JavaScript" >
<!--
var numkey = Math.random ();
Numkey = Math.Round (numkey*10000);
document.write ("File://->
</SCRIPT>

The source code is as follows:

///<summary>
///Authentication code module
///</summary>
public class CreateImage
{
public static void DrawImage ()
{
CreateImage img=new createimage ();
httpcontext.current.session["Checkcode"]=img. Rndnum (4);
IMG. Createimages (httpcontext.current.session["Checkcode"). ToString ());
}

<summary>
Generate Validation Pictures
</summary>
<param name= "Checkcode" > Validation characters </param>
private void Createimages (string checkcode)
{
int iwidth = (int) (CHECKCODE.LENGTH * 13);
System.Drawing.Bitmap image = New System.Drawing.Bitmap (iwidth, 23);
Graphics g = graphics.fromimage (image);
G.clear (Color.White);
file://define Colors
Color[] C = {Color.black,color.red,color.darkblue,color.green,color.orange,color.brown,color.darkcyan, Color.purple};
file://definition Font
string[] Font = {"Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "Song Body"};
Random rand = new Random ();
file://Random Output noise point
for (int i=0;i<50;i++)
{
int x = rand. Next (image. Width);
int y = rand. Next (image. Height);
G.drawrectangle (New Pen (Color.lightgray, 0), x,y,1,1);
}

file://to output different font and color verification code characters
for (int i=0;i<checkcode.length;i++)
{
int cindex = rand. Next (7);
int findex = rand. Next (5);

Font f = new System.Drawing.Font (Font[findex], System.Drawing.FontStyle.Bold);
Brush B = new System.Drawing.SolidBrush (C[cindex]);
int ii=4;
if ((i+1)%2==0)
{
ii=2;
}
g.DrawString (Checkcode.substring (i,1), F, B, 3+ (I*12), ii);
}
file://draw a border
G.drawrectangle (New Pen (color.black,0), 0,0,image. Width-1,image. HEIGHT-1);

file://Output to Browser
System.IO.MemoryStream ms = new System.IO.MemoryStream ();
Image. Save (Ms,system.drawing.imaging.imageformat.jpeg);
HttpContext.Current.Response.ClearContent ();
File://Response.ClearContent ();
HttpContext.Current.Response.ContentType = "Image/jpeg";
HttpContext.Current.Response.BinaryWrite (Ms. ToArray ());
G.dispose ();
Image. Dispose ();
}

<summary>
Generate a random letter
</summary>
<param name= "Vcodenum" > number of letters generated </param>
<returns>string</returns>
private string rndnum (int vcodenum)
{
String Vchar = "0,1,2,3,4,5,6,7,8,9";
string[] Vcarray = Vchar.split (', ');
String vnum = ""; file://because the string is very short, you don't have to StringBuilder.
int temp =-1; file://record the last random number and try to avoid producing a few of the same random numbers

FILE://uses a simple algorithm to ensure that random numbers are generated differently
Random Rand =new Random ();
for (int i = 1; i < vcodenum+1; i++)
{
if (temp!=-1)
{
Rand =new Random (i*temp*unchecked (int) DateTime.Now.Ticks));
}
int t = rand. Next (vcarray.length);
if (temp!=-1 && temp = t)
{
Return Rndnum (Vcodenum);
}
temp = t;
Vnum + = vcarray[t];
}
return vnum;
}
}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.