asp.net MVC Verification Code function Implementation Code _ Practical skills

Source: Internet
Author: User
Tags httpcontext rand

Front desk

Copy Code code as follows:

Height= "/>"
<span
Style= "Cursor:pointer; Text-decoration:underline "> Change a sheet </span>

Controller
Copy Code code as follows:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using SYSTEM.WEB.MVC;
Using Utility;
Using jellal**;
Namespace Sjlwebsite. Controllers
{
public class Commoncontroller:controller
{
#region Verification Code
[OutputCache (Duration = 0)]
Public ActionResult Vcode ()
{
String code = Validatecode.createrandomcode (4);
session["Vcode"] = code;
Validatecode.createimage (code);
return View ();
}

public string GetCode ()
{
return session["Vcode"]. Tostr ();
}
#endregion
}
}

Verification Code Class

Copy Code code as follows:

Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Web;
Using System.Web.SessionState;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;
Namespace Utility
{
<summary>
Perfect Random Verification Code 0.10
verion:0.10
Description: Randomly generate set verification code, and randomly rotate a certain angle, the font color is different
</summary>
public class Validatecode
{

<summary>
Generate Random Code
</summary>
<param name= "Length" > Number of random Codes www.52mvc.com</param>
<returns></returns>
public static string Createrandomcode (int length)
{
int Rand;
char code;
string randomcode = String.Empty;
Generate a certain length of verification code
System.Random Random = new Random ();
for (int i = 0; i < length; i++)
{
Rand = random. Next ();
If (rand% 3 = 0)
{
Code = (char) (' A ' + (char) (rand% 26);
}
Else
{
Code = (char) (' 0 ' + (char) (rand% 10));
}
Randomcode + = code. ToString ();
}
return randomcode;
}
<summary>
Create a random code picture
</summary>
<param name= "Randomcode" > Random code </param>
public static void CreateImage (String randomcode)
{
int randangle = 45; Random Rotation angle
int mapwidth = (int) (Randomcode. Length * 23);
Bitmap map = new Bitmap (Mapwidth, 28);//Create picture background
Graphics graph = graphics.fromimage (map);
Graph. Clear (Color.aliceblue);//erase screen, fill background
Graph. DrawRectangle (New Pen (Color.Black, 0), 0, 0, map. Width-1, map. HEIGHT-1);//Draw a border
Graph. SmoothingMode = system.drawing.drawing2d.smoothingmode.antialias;//Mode
Random rand = new Random ();
Background noise generation Www.jb51.net
Pen Blackpen = new Pen (color.lightgray, 0);
for (int i = 0; i < i++)
{
int x = rand. Next (0, map. Width);
int y = rand. Next (0, map. Height);
Graph. DrawRectangle (Blackpen, x, Y, 1, 1);
}
Verification code rotation to prevent machine recognition
char[] chars = Randomcode. ToCharArray ();//split string into single character array
In text spacing
StringFormat format = new StringFormat (stringformatflags.noclip);
Format. Alignment = Stringalignment.center;
Format. LineAlignment = Stringalignment.center;
Define Color
Color[] C = {color.black, color.red, Color.darkblue, Color.green, Color.orange, Color.brown, Color.darkcyan, COLOR.PURPL e};
Defining fonts
string[] Font = {"Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "Song Body"};
for (int i = 0; i < chars. Length; i++)
{
int cindex = rand. Next (7);
int findex = rand. Next (5);
Font f = new System.Drawing.Font (Font[findex], System.Drawing.FontStyle.Bold);//font style (parameter 2 is font size)
Brush B = new System.Drawing.SolidBrush (C[cindex]);
Point dot = new Point (16, 16);
Graph. DrawString (dot. X.tostring (), Fontstyle,new SolidBrush (Color.Black), 10,150);//test x-coordinate display spacing
float angle = rand. Next (-randangle, randangle);/the degree of rotation
Graph. TranslateTransform (dot. X, Dot. Y);/move the cursor to the specified position
Graph. RotateTransform (angle);
Graph. DrawString (chars. ToString (), F, B, 1, 1, format);
Graph. DrawString (chars. ToString (), Fontstyle,new SolidBrush (Color.Blue), 1,1,format);
Graph. RotateTransform (-angle)/Turn back
Graph. TranslateTransform (2,-dot. Y);/move the cursor to the specified position
}
Graph. DrawString (randomcode,fontstyle,new SolidBrush (Color.Blue), 2,2); Standard random Code
Generate pictures
System.IO.MemoryStream ms = new System.IO.MemoryStream ();
Map. Save (MS, SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.GIF);
HttpContext.Current.Response.ClearContent ();
HttpContext.Current.Response.ContentType = "Image/gif";
HttpContext.Current.Response.BinaryWrite (Ms. ToArray ());
Graph. Dispose ();
Map. Dispose ();
}

}
}

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.