This article is mainly for you to introduce the ASP. NET MVC authentication code class use method, with a certain reference value, interested in small partners can refer to
Verification Code Class
Namespace QJW. 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 class Validatecode {public Validatecode () {}///<summary>/////The maximum length of the verification code//</SU mmary> public int MaxLength {get {return 10;} }///<summary>///The minimum length of the Verification Code///</summary> public int MinLength {get {return 1;} }///<summary>//Generate verification codes///</summary>//<param name= "Length" > Specify the lengths of the Captcha </param> <returns></returns> public string Createvalidatecode (int length) {int[] randmembers = new int[ Length]; int[] validatenums = new Int[length]; String validatenumberstr = ""; Generate starting sequence value int seekseek = unchecked ((int) DAteTime.Now.Ticks); Random Seekrand = new Random (Seekseek); int beginseek = (int) seekrand.next (0, Int32.maxvalue-length * 10000); Int[] seeks = new Int[length]; for (int i = 0; i < length; i++) {Beginseek + = 10000; Seeks[i] = Beginseek; }//Generate random numbers for (int i = 0; i < length; i++) {Random rand = new random (seeks[i]); int pownum = 1 * (int) Math.pow (ten, length); Randmembers[i] = rand. Next (Pownum, Int32.MaxValue); }//extract random numbers for (int i = 0; i < length; i++) {string numstr = Randmembers[i]. ToString (); int numlength = Numstr.length; Random rand = new Random (); int numposition = rand. Next (0, numLength-1); Validatenums[i] = Int32.Parse (numstr.substring (numposition, 1)); }//Generate Verification code for (int i = 0; i < length; i++) {validatenumberstr + = Validatenums[i]. ToString (); } return VALIDATENUMBERSTR; }//<Summary>///Create a picture of the captcha///</summary>//<param name= "containspage" > Page object to be exported </param>/ <param name= "Validatenum" > Verification Code </param> Public byte[] Createvalidategraphic (string validatecode) { Bitmap image = new Bitmap ((int) math.ceiling (validatecode.length * 12.0), 22); Graphics g = graphics.fromimage (image); try {//Generate random generator randomly random = new random (); Empty the picture background color g.clear (color.white); The interference line for the picture is (int i = 0; i <; i++) {int x1 = random. Next (image. Width); int x2 = random. Next (image. Width); int y1 = random. Next (image. Height); int y2 = random. Next (image. Height); G.drawline (New Pen (color.silver), x1, y1, x2, y2); } font font = new Font ("Arial", FontStyle.Bold | Fontstyle.italic)); LinearGradientBrush brush = new LinearGradientBrush (new Rectangle (0, 0, image. Width, image. Height), Color.Blue, color.darkred, 1.2f, true); g.DrawString (Validatecode, Font, Brush, 3, 2); The foreground of the painting picture interferes with the point for (int i = 0; i <; i++) {int x = random. Next (image. Width); int y = random. Next (image. Height); Image. SetPixel (x, Y, Color.FromArgb (random. Next ())); }//Draw the border line of the picture G.drawrectangle (new Pen (Color.silver), 0, 0, image. Width-1, image. HEIGHT-1); Save picture Data MemoryStream stream = new MemoryStream (); Image. Save (stream, imageformat.jpeg); Output picture stream return stream. ToArray (); } finally {g.dispose (); Image. Dispose (); }}///<summary> Get the length of the CAPTCHA image////</summary>//<param name= "Validatenumlength" > The length of the Verification code Degrees </param>///<returns></returns> public static int getimagewidth (int validatenumlength) { return (int) (VALIDATENUMLENGTH * 12.0); }//<summary>//The height of the verification code///</summary> <returns></returns> public static Double Getimageheight () {return 22.5; } }}
How to use
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"); }
Html
<a href= "javascript:;" onclick= "Reloadcode ();" ></a>
View JS
function Reloadcode () { var verify = document.getElementById (' Safecode '); Verify.setattribute (' src ', '/common/createvalidate ' + math.random ()); }