Asp. The writing of the Net Verification code

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

Using System.Drawing;
Using System.Drawing.Imaging;
private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page
Rndnum is a custom function
String Vnum=rndnum (4); The number 4 here represents the 4-bit validation string shown!
session["Vnum"]=vnum;
Validatecode (Vnum);
}
Generating image functions
private void Validatecode (String vnum)
{
int gheight= (int) (VNUM.LENGTH * 11.5);
Gheight for picture width, automatically change picture width based on character length
System.Drawing.Bitmap Img = new System.Drawing.Bitmap (gheight,20);
Graphics g = graphics.fromimage (IMG);
g.DrawString (vnum,new System.Drawing.Font ("Arial"), New System.Drawing.SolidBrush (Color.Red), 3, 3);
Draws a string within a rectangle (string, font, brush color, upper left X. Upper-Left Y)
System.IO.MemoryStream ms=new System.IO.MemoryStream ();
Img.save (ms,system.drawing.imaging.imageformat.png);
Response.clearcontent (); Need to output image information to modify HTTP headers
Response.contenttype= "Image/png";
Response.BinaryWrite (Ms. ToArray ());
G.dispose ();
Img.dispose ();
Response.End ();
}


http://community.csdn.net/Expert/topic/3342/3342222.xml?temp=.5347406
Random extraction from a Vchar array in the generation of random number functions
Letter Case Sensitive
public string rndnum (int vcodenum)
{
String Vchar = "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,w,x,y,z";

string[] Vcarray = Vchar.split (', ');
String vnum = "";//because the strings are very short, you don't have to StringBuilder.
int temp =-1;//Record last random value, try to avoid producing a few of the same random numbers

A simple algorithm is used to guarantee the difference of generating random numbers
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 (35);
int T=rand. Next (35);
if (temp!=-1 && temp = t)
{
Return Rndnum (Vcodenum);
}
temp = t;
Vnum + = vcarray[t];

}
return vnum;
}
-----------------------------------End--------------------------------------------

The following is login.aspx in response to the call to the Submit button
public void doit (object sender, System.EventArgs e)
{
if (Page.IsValid)
{

String Vnum;
vnum=session["Vnum"]. ToString ();
Session.Abandon ();
viewstate["Vnum"]=vnum;
if (this. vcode.text==viewstate["Vnum"]. ToString ())
{

Hover.Manage.CheckLogin obj=new Hover.Manage.CheckLogin ();
String Name=username. Text;
String Password=formsauthentication.hashpasswordforstoringinconfigfile (pass. Text.tostring (), "MD5");
if (!obj.checklogin (Name,password))
{
Response.Redirect (".. /error.aspx?action=errorlogin ");
Response.End ();
Return
}
Else
{
Session.add ("AdminName", name);
Session.add ("AdminPass", password);
Response.Redirect ("default.aspx");
Response.End ();
Return
}

}
Else
{
Response.Write ("<script>alert" Please enter the correct add-on code!) \ ");</script>");
}
}
}
If the validation string submitted over is correct, verify that it is a legitimate user!
Otherwise pop-up prompts to return to the landing page! You can also simplify this page!
if (this. vcode.text==viewstate["Vnum"]. ToString ())
{
Response.Write ("Verify code correct");
}
Else
{
Response.Write ("Authentication code Error!");
}




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.