62,046
社区成员
发帖
与我相关
我的任务
分享
<table width="400" border="0" align="center" cellpadding="1" cellspacing="5" style="height:193px">
<tr>
<td colspan="2" align="left"><div align="left">用户名
<asp:TextBox ID="TxtName" runat="server" Width="129px" CssClass="username"></asp:TextBox>
<asp:RequiredFieldValidator ID="f_UserNameX" runat="server" ControlToValidate="TxtName" ErrorMessage="请填写用户名"></asp:RequiredFieldValidator><script language="javascript" type="text/javascript">document.getElementById('TxtName').focus();</script>
</div></td>
</tr>
<tr>
<td colspan="2" align="left"><div align="left">密 码
<asp:TextBox ID="TxtPassword" runat="server" CssClass="password" TextMode="Password" Width="129px" MaxLength="18"></asp:TextBox>
<asp:RequiredFieldValidator ID="f_PasswordX" runat="server" ControlToValidate="TxtPassword" ErrorMessage="请填写密码"></asp:RequiredFieldValidator>
</div></td>
</tr>
<tr runat="server" id="safeCodeVerify_1">
<td colspan="2" align="left"><div align="left">安全码
<asp:TextBox ID="TxtSafeCode" runat="server" CssClass="password" TextMode="Password" Width="129px" MaxLength="80"></asp:TextBox>
<asp:RequiredFieldValidator ID="f_safeCodeVerify" runat="server" ControlToValidate="TxtSafeCode" ErrorMessage="请填写安全码"></asp:RequiredFieldValidator>
</div></td>
</tr>
<tr>
<td style="width:53%;" align="left">效验码
<asp:TextBox ID="TxtVerify" CssClass="vercode" runat="server" Width="52px"></asp:TextBox>
<script type="text/javascript" language="JavaScript">
var numkey = Math.random();
numkey = Math.round(numkey*10000);
document.write("<img src=\"../comm/Image.aspx?k="+ numkey +"\" width=\"70\" onClick=\"this.src+=Math.random()\" alt=\"图片看不清?点击重新得到验证码\" style=\"cursor:pointer;\" height=\"23\" hspace=\"4\"");
</script> </td>
</tr>
<tr>
<td colspan="2" align="left" style="height:36px;">
<asp:HiddenField ID="HidUrl" runat="server" />
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="../sysImages/Login/signin.gif" OnClick="login_Click" />
<label><asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="../sysImages/Login/reset.gif" OnClientClick="javascript:window.close();" /></label></td>
</tr>
</table>
string Account = this.TxtName.Text;
string PassWord = this.TxtPassword.Text;
string VerifyCode = this.TxtVerify.Text.Trim();
string SafeCode = this.TxtSafeCode.Text;
string Urls = this.HidUrl.Value;
string SN_SERAL = this.TxtSafeCode.Text;
if (Account.Trim() == string.Empty || PassWord.Trim() == string.Empty)
{
PageError("用户与密码不能为空,详细请查看帮助说明!", "login.aspx?" + Request.QueryString);
}
if (Session["CheckCode"] == null)
{
PageError("验证码过期,请返回重新登录!", "login.aspx?" + Urls + "");
}
string CheckCode = Session["CheckCode"].ToString().ToUpper();
Session.Remove("CheckCode");
if (VerifyCode.ToUpper() != CheckCode)
{
PageError("输入验证码错误!", "login.aspx?" + Urls + "");
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
namespace Galsun.Nfdw.Web.UI
{
public class ImagePage : System.Web.UI.Page
{
/// <summary>
/// 随机码认证
/// </summary>
/// <param name="code">生成认证长度</param>
protected void DrawImage(int code)
{
Session["CheckCode"] = Galsun.Common.Rand.Number(code);
CreateImages(Session["CheckCode"].ToString());
}
/// <summary>
/// /// 生成验证图片
/// /// </summary>
/// /// <param name="checkCode">验证字符</param>
protected void CreateImages(string checkCode)
{
int iwidth = (int)(checkCode.Length * 15);
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 30);
Graphics g = Graphics.FromImage(image);
g.Clear(Color.LightCyan);
//定义颜色
Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple, Color.SkyBlue };
//定义字体
string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体", "Comic Sans MS" };
Random rand = new Random();
//随机输出噪点
for (int i = 0; i < 150; i++)
{
int x = rand.Next(image.Width);
int y = rand.Next(image.Height);
g.DrawPie(new Pen(Color.LightGray, 0), x, y, 6, 6, 1, 1);
}
//输出不同字体和颜色的验证码字符
for (int i = 0; i < checkCode.Length; i++)
{
int cindex = rand.Next(7);
int findex = rand.Next(6);
Font _font = new System.Drawing.Font(font[findex], 14, System.Drawing.FontStyle.Bold);
Brush b = new System.Drawing.SolidBrush(c[cindex]);
int ii = 4;
if ((i + 1) % 2 == 0)
{
ii = 2;
}
g.DrawString(checkCode.Substring(i, 1), _font, b, 3 + (i * 12), ii);
}
//画一个边框
g.DrawRectangle(new Pen(Color.Red, 0), 100, 0, image.Width - 1, image.Height - 1);
//输出到浏览器
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();//Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
image.Dispose();
}
}
}
public partial class Inc_CheckCode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// 在此处放置用户代码以初始化页面
this.CreateCheckCodeImage(GenerateCheckCode());
}
//产生随机数的函数
private string GenerateCheckCode()
{
int number;
string checkCode = String.Empty;
System.Random random = new Random();
//随机产生一个整数
for (int i = 0; i < 4; i++)
{
number = random.Next(65,90);
checkCode += Convert.ToChar(number);
}
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
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的背景噪音线
// 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);
//画图片的前景噪音点
// 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()));
// }
//画图片的边框线
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();
}
}
}