Message Board-a login page (with verification code)

Source: Internet
Author: User

<Body>
<Form ID = "form1" runat = "server">
<Div>
<Center>
<Table border = "1" cellpadding = "0" cellspacing = "0">
<Tr>
<TD colspan = "2" style = "color: # ffffff; Background-color: #669900; Height: 32px">
<Span style = "font-size: 24pt">
Login
</Span>
</TD>
</Tr>
<Tr>
<TD> User name: </TD>
<TD>
<Asp: textbox id = "unametextbox" runat = "server"> </ASP: textbox>
</TD>
</Tr>
<Tr>
<TD>
Password & nbsp code:
</TD>
<TD>
<Asp: textbox id = "passwordtextbox" runat = "server" textmode = "password"> </ASP: textbox>
</TD>
</Tr>
<Tr>
<TD>
Image Verification:
</TD>
<TD align = "Left">
<Asp: textbox id = "validatetextbox" runat = "server" width = "71px" Height = "25px"> </ASP: textbox>
<Asp: imagebutton id = "validateimagebutton" runat = "server"/>
</TD>
</Tr>
<Tr>
<TD colspan = "2">
<Asp: Label id = "msglabel" runat = "server" forecolor = "red" font-size = "small"> </ASP: Label>
</TD>
</Tr>
<Tr>
<TD colspan = "2">
<Asp: button id = "landbutton" runat = "server" text = "login" onclick = "landbutton_click"/>
</TD>
</Tr>
</Table>
</Center>
</Div>
</Form>
</Body>

BackgroundCode:

Public partial class login: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
This. validateimagebutton. Imageurl="Image. aspx";
}
Protected void landbutton_click (Object sender, eventargs E)
{
If (this. unametextbox. Text = "") | (this. passwordtextbox. Text = ""))
{
Msglabel. Text = "the user name and password cannot be blank! ";
}
Else
{
Sqlconnection con = dB. createconnection ();
Con. open ();
String strsql = "select upass from login where uname = '" + this. unametextbox. Text + "'";
Sqlcommand cmd = new sqlcommand (strsql, con );
Dataset DS = new dataset ();
Sqldataadapter da = new sqldataadapter (strsql, con );
Da. Fill (DS, "mytable ");

Try
{
If (this. passwordtextbox. Text =DS. Tables [0]. Rows [0]. itemarray [0]. tostring (). Trim () )
{
String curuser = This. unametextbox. text;
Session ["uname"] = This. unametextbox. Text. Trim ();
String AA = This. validatetextbox. Text. tostring ();
If (AA = convert. tostring ( Session ["image"] ) | AA = convert. tostring ( Session ["image"] ). Tolower ())
{
Response. Redirect (" Message. aspx ");
}
Else
{
Response. Write ("<SCRIPT> alert ('verification code is incorrect. Please enter it again! ') </SCRIPT>" );
}
}
Else
{
Msglabel. Text = "incorrect user name or password! ";
}
}
Catch
{
Msglabel. Text = "Sorry! The user name you entered does not exist! ";
}
Con. Close ();
}
}
}

Verification Code production page:
First, create a new page image. aspx. You only need to write background code for this page. The Code is as follows:

Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. IO;
Using system. drawing;

Public partial class image: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
// Generate verification Image
This. genimg (this.Gencode(4 ));

// Store the verification code in the session for verification as needed
Session ["image"]= This.Gencode(4 );
}

// any four verification codes
private string gencode (INT p_num)
{< br> string [] source = {" 0 "," 1 "," 2 ", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C ", "D", "E", "F", "g",
"g", "H", "I", "g", "K ", "L", "M", "n", "O", "P", "Q", "r", "S", "T", "U ", "V", "W", "X", "Y", "Z" };< br> string code = "";
random RD = new random ();

For (INT I = 0; I <p_num; I ++)
{
Code + = source [Rd. Next (0, source. length)];
}

Return code;
}

// Produce images
Private voidGenimg(String p_code)
{
// Define a canvas
BitmapMypalette = new Bitmap (60, 20 );

// Define the drawing instance on the drawing board
Graphics GH = graphics.Fromimage(Mypalette );

// Define a rectangle
Rectangle rc = new rectangle (0, 0, 60, 20 );

// Fill the rectangle
GH.Fillrectangle(New solidbrush (color.Blue), RC );

// Draw a string in the rectangle
Gh.Drawstring(P_code, New font ("", 16), new solidbrush (color. White), RC );

// Display the image
Mypalette.Save(Response. outputstream, system. Drawing. imaging. imageformat.JPEG);

Gh.Dispose();
Mypalette.Dispose();

}
}

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.