ASP. net mvc implements the verification code Function

Source: Internet
Author: User

Asp.net MVC
Front-end
Height = "25"/>
& Nbsp; <span
Style = "cursor: pointer; text-decoration: underline"> change one </span>
Controller
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
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: The verification code is randomly generated and rotated to 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 verification code of a certain length
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 Image
/// </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); // creates an image background.
Graphics graph = Graphics. FromImage (map );
Graph. Clear (Color. AliceBlue); // Clear the screen and fill in the 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 ();
// Generate www.2cto.com based on background noise
Pen blackPen = new Pen (Color. LightGray, 0 );
For (int I = 0; I <50; I ++)
{
Int x = rand. Next (0, map. Width );
Int y = rand. Next (0, map. Height );
Graph. DrawRectangle (blackPen, x, y, 1, 1 );
}
// Rotate the verification code to prevent Machine recognition
Char [] chars = randomcode. ToCharArray (); // split the string into a single character array
// Text distance
StringFormat format = new StringFormat (StringFormatFlags. NoClip );
Format. Alignment = StringAlignment. Center;
Format. LineAlignment = StringAlignment. Center;
// Define the color
Color [] c = {Color. Black, Color. Red, Color. DarkBlue, Color. Green, Color. Orange, Color. Brown, Color. DarkCyan, Color. Purple };
// Define the font
String [] font = {"Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", ""};
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], 13, System. Drawing. FontStyle. Bold); // Font style (parameter 2 indicates the 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 the display spacing of X coordinates.
Float angle = rand. Next (-randAngle, randAngle); // degrees 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); // go 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 an image
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.