ホームページ  >  記事  >  バックエンド開発  >  .net 検証コードを生成して使用する方法

.net 検証コードを生成して使用する方法

高洛峰
高洛峰オリジナル
2017-01-13 15:02:591417ブラウズ

少人数クラス: 確認コードの役割:

数年前、ほとんどの Web サイトやフォーラムなどには確認コードがありませんでした。一般のユーザーにとって、確認コードはユーザーの操作を増やし、ユーザー エクスペリエンスを低下させるだけだったためです。しかしその後、さまざまなスパム ロボット、投票ロボット、悪意のある登録ロボットが次々に出現し、Web サイトの負荷が大幅に増加し、Web サイトのデータベースに大量のジャンク データが持ち込まれるようになりました。さまざまなロボット プログラムの破壊を防ぐために、プログラマーは人間の目でのみ認識でき、プログラムでは認識されにくい検証コードを考案しました。

認証コードは絵であり、絵の内容として文字、数字、さらには漢字も含まれています。そのような絵の内容は人間の目では容易に認識できますが、プログラムでは認識できません。データベース操作 (ログイン認証、投票、投稿、返信、登録など) を実行する前に、プログラムはまずクライアントから送信された認証コードが画像の内容と同じであるかどうかを検証します。異なる場合は、データベース操作が実行されず、検証コード エラーが表示されます。このようにして、あらゆる種類のロボット プログラムがブロックされます。

しかし、コンピューターサイエンスの発展に伴い、パターン認識などの技術はますます成熟しているため、ロボットプログラムを作成する人は、画像に書かれた内容をプログラムを通じて直接認識し、サーバーに送信することができます。 , そのため、確認コードは役に立たなくなります。ロボットプログラムの認識を防ぐため、認証コードの画像生成も日々進化しており、干渉点の追加、干渉線の追加、文字の変形、角度位置の変更、色の変更…コンピュータの認識を防ぐ様々な技術も応用されています。確認コード。この 2 つの技術の競争の中で、現在私たちが目にしている検証コードは、すでに多くの人が「これはどのような検証コードなのか。人間の目にはそれが何であるかを判断できない。」と不満を抱いています。

認証コードの機能を理解した上で、認証コードの生成と使用の簡単な例を書いてみましょう

.net 検証コードを生成して使用する方法

まず認証コードを表示するページを作成し、認証コードが正しく入力されているかどうかを判定します

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <title></title>
 <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
 <script type="text/javascript">
 //点击切换验证码
 function f_refreshtype() {
 var Image1 = document.getElementById("img");
 if (Image1 != null) {
 Image1.src = Image1.src + "?";
 }
 } 
 </script>
</head>
<body>
 <form id="form1" runat="server">
 <div>
 <table>
 <tr>
 <td>
  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
 </td>
 <td>
  <img  src="png.aspx" id="img" onclick="f_refreshtype()" / alt=".net 検証コードを生成して使用する方法" >
 </td>
 <td>
  <asp:Button ID="Button1" runat="server" Text="确定" />
 </td>
 </tr>
 </table>
 </div>
 </form>
</body>
</html>

このページバックグラウンドで検証を実行します 検証用のコード

protected void Page_Load(object sender, EventArgs e)
{
//生成的验证码被保存到session中
if (Session["CheckCode"] != null)
{
string checkcode = Session["CheckCode"].ToString();
if (this.TextBox1.Text == checkcode)
{
 ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert(&#39;验证码输入正确!&#39;)", true);
}
else
{
 ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert(&#39;验证码输入错误!&#39;)", true);
}
}
 
}

検証コードの生成ページ png.aspx

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CreateCheckCodeImage(GenerateCheckCodes(4));
}
}
public void ShowAuthCode(Stream stream, out string code)
{
Random random = new Random();
code = random.Next(1000, 9999).ToString();
 
Bitmap bitmap = CreateAuthCode(code);
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Gif);
}
 
private string GenerateCheckCodes(int iCount)
{
int number;
string checkCode = String.Empty;
int iSeed = DateTime.Now.Millisecond;
System.Random random = new Random(iSeed);
for (int i = 0; i < iCount; i++)
{
number = random.Next(10);
checkCode += number.ToString();
}
Session["CheckCode"] = checkCode;
return checkCode;
}
 
private Bitmap CreateAuthCode(string str)
{
Font fn = new Font("宋体", 12);
Brush forecolor = Brushes.Black;
Brush bgcolor = Brushes.White;
PointF pf = new PointF(5, 5);
Bitmap bitmap = new Bitmap(100, 25);
Rectangle rec = new Rectangle(0, 0, 100, 25);
Graphics gh = Graphics.FromImage(bitmap);
gh.FillRectangle(bgcolor, rec);
gh.DrawString(str, fn, forecolor, pf);
return bitmap;
}
 
private void CreateCheckCodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
return;
int iWordWidth = 15;
int iImageWidth = checkCode.Length * iWordWidth;
Bitmap image = new Bitmap(iImageWidth, 20);
Graphics g = Graphics.FromImage(image);
try
{
//生成随机生成器 
Random random = new Random();
//清空图片背景色 
g.Clear(Color.White);
 
//画图片的背景噪音点
for (int i = 0; i < 20; 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);
}
 
//画图片的背景噪音线 
for (int i = 0; i < 2; i++)
{
 int x1 = 0;
 int x2 = image.Width;
 int y1 = random.Next(image.Height);
 int y2 = random.Next(image.Height);
 if (i == 0)
 {
 g.DrawLine(new Pen(Color.Gray, 2), x1, y1, x2, y2);
 }
 
}
 
 
for (int i = 0; i < checkCode.Length; i++)
{
 
 string Code = checkCode[i].ToString();
 int xLeft = iWordWidth * (i);
 random = new Random(xLeft);
 int iSeed = DateTime.Now.Millisecond;
 int iValue = random.Next(iSeed) % 4;
 if (iValue == 0)
 {
 Font font = new Font("Arial", 13, (FontStyle.Bold | System.Drawing.FontStyle.Italic));
 Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height);
 LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.Red, 1.5f, true);
 g.DrawString(Code, font, brush, xLeft, 2);
 }
 else if (iValue == 1)
 {
 Font font = new System.Drawing.Font("楷体", 13, (FontStyle.Bold));
 Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height);
 LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.DarkRed, 1.3f, true);
 g.DrawString(Code, font, brush, xLeft, 2);
 }
 else if (iValue == 2)
 {
 Font font = new System.Drawing.Font("宋体", 13, (System.Drawing.FontStyle.Bold));
 Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height);
 LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Green, Color.Blue, 1.2f, true);
 g.DrawString(Code, font, brush, xLeft, 2);
 }
 else if (iValue == 3)
 {
 Font font = new System.Drawing.Font("黑体", 13, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Bold));
 Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height);
 LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.Green, 1.8f, true);
 g.DrawString(Code, font, brush, xLeft, 2);
 }
}
//////画图片的前景噪音点 
//for (int i = 0; i < 8; i++)
//{
// int x = random.Next(image.Width);
// int y = random.Next(image.Height);
// image.SetPixel(x, y, Color.FromArgb(random.Next()));
//}
//画图片的边框线 
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.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}

.net 検証コードの生成と使用法に関連する記事の詳細については、PHP 中国語 Web サイトに注目してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。