Display the verification code interface:
Front-end:
<Td class = "style4">
Verification Code: </td>
<Td class = "style2">
<Asp: TextBox ID = "TextBox3" runat = "server" Height = "21px"> </asp: TextBox>
<Asp: Image ID = "Image1" runat = "server" ImageUrl = "~ /Default2.aspx "/>
& Nbsp; <asp: HyperLink ID = "HyperLink1" runat = "server"
NavigateUrl = "~ /Default. aspx "> cannot see the change Picture </asp: HyperLink>
</Td>
Background:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Data;
Using System. Data. SqlClient;
Using System. Configuration;
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
}
Protected void button#click (object sender, EventArgs e)
{
String str = ConfigurationManager. ConnectionStrings ["sqlstr"]. ConnectionString;
Using (SqlConnection sqlCnn = new SqlConnection (str ))
{
Using (SqlCommand sqlCmm = sqlCnn. CreateCommand ())
{
SqlCmm. CommandText = "select * from Login ";
SqlCnn. Open ();
SqlDataReader reader = sqlCmm. ExecuteReader ();
Bool bl = false;
If (reader! = Null)
{
While (reader. Read ())
{
If (this. textBox1.Text = reader ["name"]. toString () & this. textBox2.Text = reader ["password"]. toString () & this. textBox3.Text = Session ["code"]. toString ())
{
Bl = true;
}
}
If (bl = true)
{
ClientScript. RegisterClientScriptBlock (GetType (), "prompt", "<script> alert ('login successful! ') </Script> ");
}
Else
{
ClientScript. RegisterClientScriptBlock (GetType (), "prompt", "<script> alert ('login failed! ') </Script> ");
}
}
}
}
}
}
Verification Code interface:
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. IO;
Using System. Text;
Public partial class Default2: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
System. Drawing. Image img = new Bitmap (100,20 );
Graphics g = Graphics. FromImage (img );
This. DealImage (img, 10 );
G. DrawLine (Pens. Yellow, 0, 0,100, 20 );
String code = this. GenerateCode ();
Font font = new Font ("", 20, FontStyle. Bold | FontStyle. Italic | FontStyle. Strikeout | FontStyle. Underline );
Session ["code"] = code; // save to Session
G. DrawString (code, font, Brushes. YellowGreen, 0, 0 );
This. Response. Clear ();
MemoryStream MS = new MemoryStream ();
Img. Save (MS, System. Drawing. Imaging. ImageFormat. Jpeg );
This. Response. BinaryWrite (ms. ToArray ());
This. Response. Flush ();
This. Response. End ();
}
Private void DealImage (System. Drawing. Image img, int nums)
{
Bitmap B = img as Bitmap;
Random ran = new Random ();
For (int I = 0; I <nums; I ++)
{
B. SetPixel (ran. Next (0, img. Width), ran. Next (0, img. Height), Color. White );
}
}
Private string GenerateCode () // generate a random number
{
Random ran = new Random (DateTime. Now. Millisecond );
StringBuilder sb = new StringBuilder (6 );
For (int I = 0; I <6; I ++)
{
Sb. Append (ran. Next (0, 9 ));
}
Return sb. ToString ();
}
}
From blue-blue