Verification Code Class
namespaceQJW. verifycode{//Usage://Public Filecontentresult createvalidate ()//{ //Validatecode vcode = new Validatecode (); //String code = Vcode.createvalidatecode (5); //session["Validatecode"] = code; //byte[] bytes = vcode.createvalidategraphic (code); //return File (bytes, "Image/jpeg"); //} Public classValidatecode { PublicValidatecode () {}/// <summary> ///Maximum length of verification code/// </summary> Public intMaxLength {Get{return Ten; } } /// <summary> ///Minimum length of verification code/// </summary> Public intMinLength {Get{return 1; } } /// <summary> ///Generate verification Code/// </summary> /// <param name= "Length" >Specify the length of the verification code</param> /// <returns></returns> Public stringCreatevalidatecode (intlength) { int[] Randmembers =New int[length]; int[] Validatenums =New int[length]; stringValidatenumberstr =""; //generate a starting sequence value intSeekseek =unchecked((int) DateTime.Now.Ticks); Random Seekrand=NewRandom (Seekseek); intBeginseek = (int) Seekrand.next (0, Int32.maxvalue-length *10000); int[] seeks =New int[length]; for(inti =0; i < length; i++) {Beginseek+=10000; Seeks[i]=Beginseek; } //Generate random numbers for(inti =0; i < length; i++) {Random rand=NewRandom (Seeks[i]); intPownum =1* (int) Math.pow (Ten, length); Randmembers[i]=Rand. Next (Pownum, Int32.MaxValue); } //extracting random numbers for(inti =0; i < length; i++) { stringNumstr =Randmembers[i]. ToString (); intNumlength =numstr.length; Random Rand=NewRandom (); intNumposition = Rand. Next (0, Numlength-1); Validatenums[i]= Int32.Parse (numstr.substring (Numposition,1)); } //Generate verification Code for(inti =0; i < length; i++) {Validatenumberstr+=Validatenums[i]. ToString (); } returnValidatenumberstr; } /// <summary> ///Create a picture of the verification code/// </summary> /// <param name= "Containspage" >the Page object to output to</param> /// <param name= "Validatenum" >Verification Code</param> Public byte[] Createvalidategraphic (stringValidatecode) {Bitmap image=NewBitmap ((int) math.ceiling (Validatecode.length *12.0), A); Graphics g=graphics.fromimage (image); Try { //Generate random GeneratorsRandom random =NewRandom (); //clear the background color of the pictureg.clear (Color.White); //interference lines for drawing pictures for(inti =0; I < -; i++) { intX1 =Random. Next (image. Width); intx2 =Random. Next (image. Width); intY1 =Random. Next (image. Height); inty2 =Random. Next (image. Height); G.drawline (NewPen (color.silver), x1, y1, x2, y2); } Font Font=NewFont ("Arial", A, (FontStyle.Bold |fontstyle.italic)); LinearGradientBrush Brush=NewLinearGradientBrush (NewRectangle (0,0, image. Width, image. Height), Color.Blue, color.darkred,1.2f,true); g.DrawString (Validatecode, font, brush,3,2); //foreground interference point of picture painting for(inti =0; I < -; i++) { intx =Random. Next (image. Width); inty =Random. Next (image. Height); Image. SetPixel (x, Y, Color.FromArgb (random. Next ())); } //draw the border line of a pictureG.drawrectangle (NewPen (Color.silver),0,0, image. Width-1, image. Height-1); //Save picture DataMemoryStream stream =NewMemoryStream (); Image. Save (stream, imageformat.jpeg); //Output Picture Stream returnStream. ToArray (); } finally{g.dispose (); Image. Dispose (); } } /// <summary> ///get the length of the Verification code picture/// </summary> /// <param name= "Validatenumlength" >the length of the verification code</param> /// <returns></returns> Public Static intGetimagewidth (intvalidatenumlength) { return(int) (Validatenumlength *12.0); } /// <summary> ///get the height of the verification code/// </summary> /// <returns></returns> Public Static Doublegetimageheight () {return 22.5; } }}
How to use
public Filecontentresult Createvalidate ( {Validatecode Vcode = new V Alidatecode (); string code = vcode.createvalidatecode ( 5 ); session[ validatecode "] = code; byte [] bytes = return File (bytes, " image/jpeg ); }
Html
<href= "javascript:;" onclick = "Reloadcode ();" >< ID= "Safecode" src= "/common/createvalidate" alt= ""/></a>
View JS
function Reloadcode () { var verify = document.getElementById (' Safecode '); Verify.setattribute (' src ', '/common/createvalidate ' + math.random ()); }
ASP. NET MVC Authentication Code