asp.Netログイン認証コード
3833 ワード
フロントコード
検証コード生成ページ:ValidCode.aspx
<form method="POST" action="CheckUser.aspx" id="fromLogin">
<table>
<tr>
<td><img src="ValidCode.aspx" alt=' , 。'ondblclick=" this.src = 'ValidCode.aspx?flag=' + Math.random() " border="1" height="44" width="126"/></td>
</tr>
</table>
</form>
検証コード生成ページ:ValidCode.aspx
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Web.UI;
namespace CST.WebApp
{
public partial class ValidCode : Page
{
private readonly Random ran = new Random();
protected void Page_Load(object sender, EventArgs e)
{
string str = getRandomValidate(5);
Session["check"] = str;
// Session, , , cookie
getImageValidate(str);
}
// ,
private string getRandomValidate(int len)
{
int num;
int tem;
string rtuStr = "";
for (int i = 0; i < len; i++)
{
num = ran.Next();
if (i%2 == 0)
{
tem = num%10 + '0'; //
}
else
{
tem = num%26 + 'A'; //
}
rtuStr += Convert.ToChar(tem).ToString() + " ";
}
return rtuStr;
}
//
private void getImageValidate(string strValue)
{
//string str = "OO00"; // O, 0
//int width = Convert.ToInt32(strValue.Length*12); //
// var img = new Bitmap(width, 141);
var img = new Bitmap(150, 42);
Graphics gfc = Graphics.FromImage(img); // Graphics ,
gfc.Clear(Color.White);
drawLine(gfc, img);
// , Font
var font = new Font("Brush Script Std", 16);
var brush =
new LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.DarkOrchid, Color.Blue, 1.5f,
true);
gfc.DrawString(strValue, font, brush, 20, 9);
drawPoint(img);
gfc.DrawRectangle(new Pen(Color.DarkGray), 0, 0, img.Width - 1, img.Height - 1);
//
var ms = new MemoryStream();
img.Save(ms, ImageFormat.Gif);
// Http
Response.ClearContent();
Response.ContentType = "image/gif";
Response.BinaryWrite(ms.ToArray());
//Dispose
gfc.Dispose();
img.Dispose();
Response.End();
}
private void drawLine(Graphics gfc, Bitmap img)
{
// 10 , , ,
for (int i = 0; i < 10; i++)
{
int x1 = ran.Next(img.Width);
int y1 = ran.Next(img.Height);
int x2 = ran.Next(img.Width);
int y2 = ran.Next(img.Height);
gfc.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); // ,
}
}
private void drawPoint(Bitmap img)
{
int col = ran.Next(); //
for (int i = 0; i < 100; i++)
{
int x = ran.Next(img.Width);
int y = ran.Next(img.Height);
img.SetPixel(x, y, Color.FromArgb(col));
}
}
}
}