You can also add a text verification code to the login control.

Source: Internet
Author: User

Expand the login control provided by Asp.net, and add a text verification function after the password to Prevent Automatic Registration of programs.
1. Add imagevalidate. aspx and add information to the header.
<% Response. Buffer = true; %>
<% Response. expiresabsolute = datetime. Now. addseconds (-1); %>
<% Response. expires = 0; %>
<% Response. cachecontrol = "no-Cache"; %>
The code in imagevalidate. aspx. CS is as follows:
Protected void page_load (Object sender, eventargs E)
{
// Generate a four-digit Verification Code
/* String TMP = rndnum (4 );
Httpcookie cookie = new httpcookie ("imagev", TMP );
// Response. Cookies ["imagev"]. value = TMP;
Response. Cookies. Add (cookie );
This. validatecode (TMP );*/

Createcheckcodeimage (generatecheckcode ());

}

Private string generatecheckcode ()
{
Int number;
Char code;
String checkcode = string. empty;

System. Random random = new random ();

For (INT I = 0; I <5; I ++)
{
Number = random. Next ();

If (Number % 2 = 0)
Code = (char) ('0' + (char) (Number % 10 ));
Else
Code = (char) ('A' + (char) (Number % 26 ));

Checkcode + = code. tostring ();
}
Session ["checkcode"] = checkcode;


// Response. Cookies. Add (New httpcookie ("checkcode", checkcode ));

Return checkcode;
}

Private void createcheckcodeimage (string checkcode)
{
If (checkcode = NULL | checkcode. Trim () = string. Empty)
Return;

System. Drawing. bitmap image = new system. Drawing. Bitmap (INT) math. Ceiling (checkcode. length * 12.5), 22 );
Graphics G = graphics. fromimage (image );

Try
{
// Generate a random Generator
Random random = new random ();

// Clear the background color of the image
G. Clear (color. White );

// Draw the background noise line of the image
For (INT I = 0; I <25; 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 system. Drawing. Font ("Arial", 12, (system. Drawing. fontstyle. Bold | system. Drawing. fontstyle. italic ));
System. drawing. drawing2d. lineargradientbrush brush = new system. drawing. drawing2d. lineargradientbrush (New rectangle (0, 0, image. width, image. height), color. blue, color. darkred, 1.2f, true );
G. drawstring (checkcode, Font, brush, 2, 2 );

// Foreground noise of the image
For (INT I = 0; I <100; 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 the image
G. drawrectangle (new pen (color. Silver), 0, 0, image. Width-1, image. Height-1 );

System. Io. memorystream MS = new system. Io. memorystream ();
Image. Save (MS, system. Drawing. imaging. imageformat. GIF );
Response. clearcontent ();
Response. contenttype = "image/GIF ";
Response. binarywrite (Ms. toarray ());
}
Finally
{
G. Dispose ();
Image. Dispose ();
}
}
This is the page for generating the verification code
2. Add the page to the login control:
Drag a login control to switch to the mode. In the HTML source file, add the following lines to the password row in the table:
<Tr>
<TD style = "width: 151px">
<Asp: Label id = "label1" runat = "server" associatedcontrolid = "password"> Verification Code: </ASP: Label>
</TD>
</Tr>
<Tr>
<TD style = "width: 151px">
<Asp: textbox id = "txtimgvalid" runat = "server" font-size = "0.8em"> </ASP: textbox>
<Asp: imagebutton id = "imgvalid" imageurl = "~ /Public/imagevalidate. aspx "runat =" server "/>
</TD>
</Tr>

Here, imageurl = "~ The imagevalidate. aspx of/public/imagevalidate. aspx indicates the file created in step 1.
3. Add code to the authenticate event of the login Control
Protected void login=authenticate (Object sender, authenticateeventargs E)
{
Textbox txtimage = login1.findcontrol ("txtimgvalid") as textbox;


If (session ["checkcode"] = NULL)
{
Response. Write ("<script language = 'javascript '> alert ('unknown error! ') </SCRIPT> ");
E. Authenticated = false;
Return;
}

If (string. Compare (session ["checkcode"]. tostring (), txtimage. Text, true )! = 0)
{
Response. Write ("<script language = 'javascript '> alert (' does not match! ') </SCRIPT> ");
E. Authenticated = false;
Return;
}

E. Authenticated = true;

}

Click the log on button to clear the session.
Protected void loginbutton_click (Object sender, eventargs E)
{
Session ["checkcode"] = NULL;
}
Running result

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.