//login.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="admin_login" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>登录</title> <link href="Ext/resources/css/ext-all.css" type="text/css" rel="stylesheet" /> <style type="text/css"> .user{ background:url(images/user.gif) no-repeat 1px 2px; } .key{ background:url(images/key.gif) no-repeat 1px 2px; } .key,.user{ background-color:#FFFFFF; padding-left:20px; font-weight:bold; color:#000033; } </style> <script src="Ext/adapter/ext/ext-base.js" type="text/javascript"></script> <script src="Ext/ext-all.js" type="text/javascript"></script> </head> <body> <script type="text/javascript" language="javascript"> Ext.QuickTips.init(); Ext.form.Field.prototype.msgTarget = 'side'; LoginWindow=Ext.extend(Ext.Window,{ title : '登陆系统', width : 265, height : 170, closeAction:'hide', closable: false, collapsible : true, listeners:{ "hide":function(){ }, "show":function(LoginWindow){LoginWindow.findByType("textfield")[3].getEl().dom.src="ValidateCode.aspx";} }, defaults : { border : false }, buttonAlign : 'center', createFormPanel :function() { return new Ext.form.FormPanel( { bodyStyle : 'padding-top:10px', defaultType : 'textfield', labelAlign : 'right', labelWidth : 55, labelPad : 0, frame : true, defaults : { allowBlank : false, width : 158 }, items : [{ cls : 'user', name : 'userName', fieldLabel : '帐号', blankText : '帐号不能为空' }, { cls : 'key', name : 'password', fieldLabel : '密码', blankText : '密码不能为空', inputType : 'password' },{ fieldLabel:"验证", xtype:"panel", baseCls:"x-plain", layout:"column", defaultType:"textfield", items:[ { columnWidth:.65, xtype:"textfield", cls : 'code', name : 'validateCode', blankText : '验证码不能为空', allowBlank: false, style: 'background-color:#FFFFFF;font-weight:bold;color:#000033;padding-left:20px;' }, {columnWidth:.04,xtype:"panel",baseCls:"x-plain"}, { columnWidth:.31, layout:"form", inputType:"image", height:18, 50, id:'verify_code', name:'verify_code', listeners:{ "focus":function(){ this.ownerCt.ownerCt.findByType("textfield")[2].focus(); this.ownerCt.ownerCt.findByType("textfield")[3].getEl().dom.src="ValidateCode.aspx?t="+new Date(); } } }] }] }); }, login:function() { if(this.fp.form.isValid()){ this.fp.form.submit({ url : 'Portal.aspx', waitMsg : '正在登录......', method: 'POST', success : function(form, action) { var loginResult = action.result.success; if(loginResult == false){ Ext.Msg.alert('提示', action.result.msg); } else { if(loginResult == true){ window.location.href = 'Default.aspx'; } } }, failure : function(form, action) { form.reset(); switch (action.failureType){ case Ext.form.Action.CLIENT_INVALID: // Ext.Msg.alert("Failure", "Form fields may not be submitted with invalid values"); Ext.Msg.alert("错误", "提交的表单数据无效,请检查!"); break; case Ext.form.Action.CONNECT_FAILURE: // Ext.Msg.alert("Failure", "Ajax communication failed"); Ext.Msg.alert("错误", "服务器连接失败,请稍后再试!"); break; case Ext.form.Action.SERVER_INVALID: // Ext.Msg.alert("Failure", action.result.msg); Ext.Msg.alert("错误", action.result.msg); } } }); } }, initComponent : function(){ this.keys={ key: Ext.EventObject.ENTER, fn: this.login, scope: this}; LoginWindow.superclass.initComponent.call(this); this.fp=this.createFormPanel(); this.add(this.fp); this.addButton('登陆',this.login,this); this.addButton('重置', function(){this.fp.form.reset();},this); } }); Ext.onReady(function() { var win=new LoginWindow(); win.show(); new Ext.ToolTip({target: 'verify_code', 120, html: '看不清? 点击刷新',trackMouse:true}); } ); </script> </body> </html>
//Portal.aspx.cs
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class Form_Portal : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { CheckUser(); } } private void CheckUser() { string name = Request.Params["userName"]; string pwd = Request.Params["password"]; string code = Request.Params["validateCode"]; if (code != Session["CheckCode"].ToString()) { Response.Write("{\"success\":false,msg:\"验证码错误!\"}"); } else { if (name != "" & pwd != "") { bool a = ret(); if (a) { Session["LoginName"] = name; Response.Write("{\"success\":true,msg:\"登录成功!\"}"); } else { Response.Write("{\"success\":false,msg:\"用户名或密码错误!\"}"); } } } } public bool ret() { bool flag = false; string name = Request.Params["userName"]; string pwd = Request.Params["password"]; db s = new db(); int count = 0; count = s.ReturnSQL("select count(*) from admin where userName='" + name + "' and userPass='" + pwd + "'"); if( count>0) { flag= true; } return flag; } }
//ValidateCode.aspx.cs
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Drawing; public partial class ValidateCode : System.Web.UI.Page { private void Page_Load(object sender, System.EventArgs e) { //每次载入时执行创建验证码图像并填写验证码字符函数,保证每次刷新的验证码都不同。 this.CreateCheckCodeImage(GenerateCheckCode()); } #region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion private string GenerateCheckCode() //产生随机验证码字符函数 { int number; char code; string checkCode = String.Empty; System.Random random = new Random(); for (int i = 0; i < 4; i++) //字符和数字的混合.长度为4。其实i的大小可以自由设置 { number = random.Next(); if (number % 2 == 0) //偶数 code = (char)('0' + (char)(number % 10)); else code = (char)('A' + (char)(number % 26)); checkCode += code.ToString(); } //esponse.Cookies.Add(new HttpCookie("CheckCode", checkCode)); //把产生的验证码保存到COOKIE中 Session["CheckCode"] = checkCode.ToLower(); return checkCode;//返回结果以供CreateCheckCodeImage()函数使用 } //以下函数是创建验证码图像并填写验证码字符 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);//用指定的大小初始化Bitmap类创建图像对象 Graphics g = Graphics.FromImage(image);//表示在指定的图像上写字 try { //生成随机生成器 Random random = new Random(); //清空图片背景色 g.Clear(Color.White); //画图片的背景噪音线 for (int i = 0; i < 2; 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); //用画笔画出高级的2D向量图形字体 g.DrawString(checkCode, font, brush, 2, 2);//以字符串的形式输出 //画图片的前景噪音点 for (int i = 0; i < 13; i++)//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); //把生成的gif以字节数组的形式保存在内存里 Response.ClearContent();//清空缓冲区中的内容 Response.ContentType = "image/Gif";//输出内容的类型,即以gif的文件格式输出 Response.BinaryWrite(ms.ToArray()); //把字节数组一二进制的形式输出,即以基本数据类型形式输出 } finally { g.Dispose();//释放绘图对象 image.Dispose();//释放图像对象 } } } //Request.Cookies["CheckCode"].Value