MVC3 Verification Code

Source: Internet
Author: User

Public ActionResult Getvalidatecode ()
{

String code = Createvalidatecode (5);
session["Validatecode"] = code;
byte[] bytes = createvalidategraphic (code);
Return File (bytes, @ "Image/jpeg");
}

<summary>
Generate Verification Code
</summary>
<param name= "Length" > Specified verification code lengths </param>
<returns></returns>
public string Createvalidatecode (int length)
{
int[] randmembers = new Int[length];
int[] validatenums = new Int[length];
String validatenumberstr = "";
Generate a 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);
}
Extracting 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 verification code
</summary>
<param name= "Containspage" > Page object to export to </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 Generators
Random random = new random ();
Clear the background color of the picture
G.clear (Color.White);
Interference lines for drawing pictures
for (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", page, (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);
Foreground interference point of picture painting
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 a 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 ();
}
}

$ (function () {
$ ("#valiCode"). Bind ("click", Function () {
THIS.SRC = "/public/getvalidatecode?time=" + (new Date ()). GetTime ();
});
});</script>

There is also a well-packaged MVC verification code component, which is also very useful.

http://www.webdiyer.com/mvccaptcha/

Reference Original: http://www.cnblogs.com/fumj/archive/2012/07/08/2581561.html

MVC3 Verification Code

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.