First create an ASP. NET page validatecode.aspx
Do not write anything:
<%@ page language= "C #" autoeventwireup= "true" codebehind= "VerifyCode.aspx.cs" inherits= "Rrbmanage.verifycode"% >
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title></title>
<body>
<form id= "Form1" runat= "Server" >
<div>
</div>
</form>
</body>
Write the code in the background ValidateCode.cs file on the page:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Drawing;
Using System.Drawing.Imaging;
Namespace Rrbmanage
{
public partial class VerifyCode:System.Web.UI.Page
{
Static string[] Fontitems = new string[] {"Arial", "script", "Chinese Xingkai", "Helvetica", "Song Body", "regular script", "Seal Script", "Yan book", "Blackbody", "Running script", "imitation",
"Geneva",
"Sans-serif",
"Verdana"
};
Static brush[] Brushitems = new brush[] {brushes.olivedrab,
Brushes.forestgreen,
Brushes.darkcyan,
Brushes.lightslategray,
Brushes.royalblue,
Brushes.slateblue,
Brushes.darkviolet,
Brushes.mediumvioletred,
Brushes.indianred,
Brushes.firebrick,
Brushes.chocolate,
Brushes.peru,
Brushes.goldenrod
};
Static string[] Brushname = new string[] {"Olivedrab",
"Forestgreen",
"Darkcyan",
"Lightslategray",
"Royalblue",
"Slateblue",
"Darkviolet",
"Mediumvioletred",
"Indianred",
"Firebrick",
"Chocolate",
"Peru",
"Goldenrod"
};
private static Color BackColor = Color.White;
private static Pen bordercolor = Pens.darkgray;
private static int Width = 52;
private static int Height = 21;
Private Random _random;
private string _code;
private int _brushnameindex;
Override protected void OnInit (EventArgs e)
{
Codegen:this call was required by the Web Form Designer.
InitializeComponent ();
Base. OnInit (e);
}
/**/
/**/
/**/
<summary>
Required method for Designer support-do not modify
The contents of this method with the Code editor.
</summary>
private void InitializeComponent ()
{
This. Load + = new System.EventHandler (this. Page_Load);
}
/**/
<summary>
///
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
public void Page_Load (object sender, System.EventArgs e)
{
if (! IsPostBack)
{
//
Todo:initialize
//
This._random = new Random ();
This._code = Getrandomcode ();
//
Todo:use session["Code"] Save the Verifycode
//
session["Code" = This._code;
HttpCookie Cokie = new HttpCookie ("Rrbcode");
Cokie. Value = This._code;
Cokie. Expires = DateTime.Now.AddMinutes (10);
HTTPCONTEXT.CURRENT.RESPONSE.COOKIES.ADD (Cokie);
//
Todo:output Image
//
This. Setpagenocache ();
This. OnPaint ();
}
}
/**/
/**/
/**/
<summary>
Settings page is not cached
</summary>
private void Setpagenocache ()
{
Response.Buffer = true;
Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds (-1);
Response.Expires = 0;
Response.CacheControl = "No-cache";
Response.appendheader ("Pragma", "No-cache");
}
/**/
/**/
/**/
<summary>
Get a random code of 4 bits
</summary>
<returns></returns>
private String Getrandomcode ()
{
Return Guid.NewGuid (). ToString (). Substring (0, 4). Replace ("0", "a");
}
/**/
/**/
/**/
<summary>
Randomly take a font
</summary>
<returns></returns>
Private Font GetFont ()
{
int fontindex = _random. Next (0, fontitems.length);
FontStyle FontStyle = Getfontstyle (_random. Next (0, 60));
return new Font (Fontitems[fontindex], fontstyle);
}
/**/
/**/
/**/
<summary>
Take the style of a font
</summary>
<param name= "Index" ></param>
<returns></returns>
Private FontStyle getfontstyle (int index)
{
Switch (index)
{
Case 0:
return fontstyle.bold;
Case 1:
return fontstyle.italic;
Default
return fontstyle.regular;
}
}
/**/
/**/
/**/
<summary>
Take a random brush
</summary>
<returns></returns>
Private Brush Getbrush ()
{
int brushindex = _random. Next (0, brushitems.length);
_brushnameindex = Brushindex;
return Brushitems[brushindex];
}
/**/
/**/
/**/
<summary>
Painting events
</summary>
private void OnPaint ()
{
Bitmap objbitmap = null;
Graphics g = null;
Try
{
Objbitmap = new Bitmap (Width, Height);
g = Graphics.fromimage (Objbitmap);
Paint_background (g);
Paint_text (g);
Paint_textstain (OBJBITMAP);
Paint_border (g);
Paint_textstain (OBJBITMAP);
Objbitmap.save (Response.outputstream, imageformat.gif);
Response.ContentType = "Image/gif";
}
Catch {}
Finally
{
if (null! = Objbitmap)
Objbitmap.dispose ();
if (null! = g)
G.dispose ();
}
}
/**/
/**/
/**/
<summary>
Painting background color
</summary>
<param name= "G" ></param>
private void Paint_background (Graphics g)
{
G.clear (BackColor);
}
/**/
/**/
/**/
<summary>
Painting border
</summary>
<param name= "G" ></param>
private void Paint_border (Graphics g)
{
G.drawrectangle (bordercolor, 0, 0, Width-1, Height-1);
}
/**/
/**/
/**/
<summary>
Painting text
</summary>
<param name= "G" ></param>
private void Paint_text (Graphics g)
{
g.DrawString (_code, GetFont (), Getbrush (), 3, 1);
}
/**/
/**/
/**/
<summary>
Drawing text Noise Point
</summary>
<param name= "G" ></param>
private void Paint_textstain (Bitmap b)
{
for (int n = 0; n <; n++)
{
int x = _random. Next (Width);
int y = _random. Next (Height);
B.setpixel (x, Y, Color.fromname (Brushname[_brushnameindex]));
}
}
}
}
To invoke the verification code page Login.asp write the following code:
<%@ page language= "C #" autoeventwireup= "true" codebehind= "Login.aspx.cs" inherits= "Rrbmanage.login"%>
<!doctype html>
<meta charset= "Utf-8" >
<title> Everyone's Backstage login </title>
<link href= "Css/admin.css" type= "Text/css" rel= "stylesheet" >
<script src= "Http://localhost:57692/js/jquery.js" ></script>
<script type= "Text/javascript" >
Add Administrator Authentication
function Checkadminlogininput () {
var name = document.getElementById ("txtUserName"). Value;
var pass = document.getElementById ("Txtpassword"). Value;
var Yzm = document.getElementById ("Txtyzm"). Value;
if (Name.length = = 0 | | Pass.length < 6 | | Yzm.length = = 0) {
Alert (' The items cannot be empty! ');
return false;
}
}
function Changeimage () {
Click the trigger picture Reload event to complete the replacement of the picture verification code
document.getElementById ("Imgrandom"). src = document.getElementById ("Imgrandom"). src + '? ';
}
</script>
<body>
<div class= "Dengl" >
<form id= "Form1" runat= "Server" >
<div class= "Dltiao" >
<div class= "Dltiao1" >
</div>
<div class= "Dltiao2" >
<p> Everyone security backstage system </p>
<b>the BACKGROUND SYSTEM </b>
</div>
</div>
<div class= "Dlkuang" >
<div class= "Dlkuang1" >LOGIN</div>
<div class= "Dlkuang2" >
<span> Account Number </span>
<asp:textbox id= "txtUserName" runat= "Server" tabindex= "1" width= "></asp:TextBox>"
</div>
<div class= "Dlkuang3" >
<span> Secret Code </span>
<asp:textbox id= "Txtpassword" runat= "Server" textmode= "Password" width= "></asp:TextBox>"
</div>
<div class= "Dlkuang4" >
<span> Verification Code </span>
<asp:textbox id= "Txtyzm" runat= "Server" cssclass= "YZ" width= "$" ></asp:textbox>Src= "/verifycode.aspx" height= "id=" Imgrandom "onclick=" width= "$"/"Changeimage"/>
<p class= "Dl_an" >
</div>
<div class= "Dlkuang5" >
<span>
<%=viewstate["ErrorInfo"]%></span>
<asp:button id= "Button1" runat= "server" text= "Login Now" width= "230"
onclientclick= "return Checkadminlogininput ()" onclick= "Button1_Click"/>
</div>
</div>
</form>
</div>
</body>
The code for the background login.csde of the page that invokes the CAPTCHA is as follows:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using DAL;
Using Model;
Namespace Rrbmanage
{
protected void Button1_Click (object sender, EventArgs e)
{
String name = This.txtUserName.Text.Trim ();
String pass = This.txtPassWord.Text.Trim ();
String yzm = This.txtYzm.Text.Trim ();
HttpCookie Cokie = request.cookies["Rrbcode"];
String code = Cokie. Value;
if (name. Length = = 0 | | Pass. Length = = 0| | Yzm. length==0)
{
Jscript.alert ("The items cannot be empty");
return;
}
if (code. ToString ()! = Yzm)
{
viewstate["errorinfo"] = "Wrong verification code!" ";
return;}
Else
{
Jscript.alertandredirect ("No this account or has been deactivated, please contact the Super Administrator!") ", this. Request.rawurl);
}
}
}
ASP. NET Verification Code