In MVC, comments and other functions, will inevitably use the form to submit the verification code problem, sometimes, our practice is, when the form is submitted, in the Controller to determine the correctness of the verification code, but I think this user experience is very poor, today just have the time, Sum up the things in this way:
First, create a type validatecode of the image verification code in the public project, with the following code:
<summary>
Generate a Captcha object
</summary>
public class Validatecode
{
Public Validatecode ()
{
}
<summary>
Maximum length of verification code
</summary>
public int MaxLength
{
get {return 10;}
}
<summary>
Minimum length of verification code
</summary>
public int MinLength
{
get {return 1;}
}
<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 * 14.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", +, (FontStyle.Bold | Fontstyle.italic));
LinearGradientBrush brush = new LinearGradientBrush (new Rectangle (0, 0, image. Width, image. Height),
Color.Blue, color.darkred, 1.5f, 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 ();
}
}
}
Then, add an action that returns a file type to the current public controller, or it can be the base class for all return types ActionResult, which is used to return an image object:
<summary>
Generate Authenticode Image Object
</summary>
<returns></returns>
Public ActionResult Getvalidatecode ()
{
Validatecode Vcode = new Validatecode ();
String code = Vcode.createvalidatecode (4);
session["Validatecode"] = code;
byte[] bytes = vcode.createvalidategraphic (code);
Return File (bytes, @ "Image/jpeg");
}
Again, we need to write a method that returns the current validation, which is the key to the no-refresh verification code:
<summary>
Get Current Verification Code
</summary>
<returns></returns>
Public ActionResult Getcurrentvalidatecode ()
{
Return Content (session["Validatecode"). ToString ());
}
Finally, the thing on the page, look at the code:
<div id= "Validatecodespan" >
Please enter the CAPTCHA: @Html. TextBox ("Vcode")
@Html. Hidden ("Validatecode")
<script type= "Text/javascript" >
$ (function () {
First load
$ ("#valiCode"). attr ("src", "/common/getvalidatecode?time=" + (new Date ()). GetTime ());
$.get ("/common/getcurrentvalidatecode", function (data) {
$ ("#ValidateCode"). val (data);
});
Click the Captcha Event
$ ("#valiCode"). Bind ("click", Function () {
THIS.SRC = "/common/getvalidatecode?time=" + (new Date ()). GetTime ();
$.get ("/common/getcurrentvalidatecode", function (data) {
$ ("#ValidateCode"). val (data);
});
});
});
</script>
</div>
ASP. NET MVC3 implement no refresh verification code