//(1)登录前台页面
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="WebApplicationUI.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>
<script type="text/javascript">
//这个方法最简单的应用就是登录框,很多登录框输入了姓名密码后按回车
//可以自动聚焦登录按钮并提交表单,js如下
//网页内按下回车触发
function document.onkeydown() {
if (event.keyCode == 13) {
document.getElementById("btnClick").click();
return false;
}
}
//弹出测试按钮
function btnClick() {
//alert("测试按钮");
var name = document.getElementById("txtName");
var pwd = document.getElementById("txtPWD");
if (name.value.length > 2) {
alert("用户名不正确");
return false;
}
if (pwd.value != 123) {
alert("密码不正确");
}
}
</script>
</head>
<body οnlοad="document.onkeydown()">
<form id="form1" runat="server">
<div style="width:260px; background-color:Green; height:100px; position:absolute; top:30%; left:30%;">
<table>
<tr>
<td> 用户名: </td><td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox> </td>
</tr>
<tr>
<td align="right"> 密码: </td><td>
<asp:TextBox ID="txtPWD" runat="server" TextMode="Password"></asp:TextBox> </td>
</tr>
<tr>
<td>验证码:</td>
<td>
<asp:TextBox ID="txtValit" runat="server" Width="84px"></asp:TextBox><img src="ValidateCode.aspx" alt="验证码" /></td>
</tr>
<tr>
<td colspan="2">
<center>
<asp:Button ID="btnClick" runat="server" Text="提交" οnclick="btnClick_Click" />
</center>
<%-- <center>
<input type="button" value="提交" οnclick="btnClick()" />
</center>--%>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
//(2)登录页面后台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplicationUI
{
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
//提交功能1
/*
protected void btnClick_Click(object sender, EventArgs e)
{
// Response.Redirect("~/在一个页面中创建动态层.aspx");
//第一种方法:简单登录模式
//在登录页面使用这种方式的话就无法实现多用户权限管理了,所以需要在登录成功的模块把登录者的model封装到session里面,这样可以方便在后面的页面根据当前登录用户的身份和权限来做一些策略判断
string strCount="";//设置一个用于接收返回值的变量
MODEL.sysUser us = new MODEL.sysUser();
BLL.sysUserBLL bll = new BLL.sysUserBLL();
us.FUserName = txtName.Text;
us.FPwd = txtPWD.Text;
strCount= bll.getUser(us);
string valitstr= txtValit.Text.Trim().ToUpper();//拿到输入字母的大写验证码,提高用户体验
//这里的验证码技术就相当于是一种额外的第三方验证一样,通过服务器端来进行验证
if (valitstr!=Session["CheckCode"].ToString())//验证码存放在session当中
{
ScriptManager.RegisterStartupScript(this.Page, GetType(), "test", "alert('验证码不正确')", true);
return;//如果验证码不正确的话就没有必要校验密码和用户名了,通过这一关的考验才能够往下进行
}
if (strCount == "1")
{
//ClientScript.RegisterClientScriptBlock(GetType(), "test", "alert('用户名存在')", true);
Response.Redirect("~/Content.aspx");
}
else
{
ClientScript.RegisterClientScriptBlock(GetType(),"t1","alert('用户名或密码错误')",true);
}
}
*/
protected void btnClick_Click(object sender, EventArgs e)
{
string name = txtName.Text;//得到用户名
string pwd = txtPWD.Text;//得到密码
LoginUsercs loginuser = new LoginUsercs();//得到登录用户model对象
if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(pwd))
{
BLL.LoguserBll log = new BLL.LoguserBll();
DataTable table = log.getLoginData(name, pwd);
if (txtValit.Text.ToUpper() != Session["CheckCode"].ToString())
{
ClientScript.RegisterClientScriptBlock(GetType(), "onload", "alert('验证码不正确')", true);
return;
}
if (table != null && table.Rows != null && table.Rows.Count > 0)
{
loginuser.Name = table.Rows[0]["FUserName"].ToString();
loginuser.Pwd = table.Rows[0]["FPWD"].ToString();
loginuser.RoleId = Convert.ToInt32(table.Rows[0]["FRoleID"]);
Session["loginUser"] = loginuser;//把得到的登录用户model放入到session中
Response.Redirect("~/TreeViewTest.aspx");
}
}
else
{
ClientScript.RegisterClientScriptBlock(GetType(), "onload", "alert('登录用户名或密码错误')", true);
}
}
}
}
//(3)验证码前台页面
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ValidateCode.aspx.cs" Inherits="WebApplicationUI.ValidateCode" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
//(4)验证码后台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
namespace WebApplicationUI
{
public partial class ValidateCode : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
string checkCode = GetRandomCode(4);
Session["CheckCode"] = checkCode;
SetPageNoCache();
CreateImage(checkCode);
}
/// <summary>
/// 设置页面不被缓存
/// </summary>
private void SetPageNoCache()
{
Response.Buffer = true;
Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
Response.Expires = 0;
Response.CacheControl = "no-cache";
Response.AppendHeader("Pragma", "No-Cache");
}
private string CreateRandomCode(int codeCount)
{
string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,i,J,K,M,N,P,Q,R,S,T,U,W,X,Y,Z";
string[] allCharArray = allChar.Split(',');
string randomCode = "";
int temp = -1;
Random rand = new Random();
for (int i = 0; i < codeCount; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
}
int t = rand.Next(35);
if (temp == t)
{
return CreateRandomCode(codeCount);//性能问题
}
temp = t;
randomCode += allCharArray[t];
}
return randomCode;
}
private string GetRandomCode(int CodeCount)
{
string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,i,J,K,M,N,P,Q,R,S,T,U,W,X,Y,Z";
string[] allCharArray = allChar.Split(',');
string RandomCode = "";
int temp = -1;
Random rand = new Random();
for (int i = 0; i < CodeCount; i++)
{
if (temp != -1)
{
rand = new Random(temp * i * ((int)DateTime.Now.Ticks));
}
int t = rand.Next(33);
while (temp == t)
{
t = rand.Next(33);
}
temp = t;
RandomCode += allCharArray[t];
}
return RandomCode;
}
private void CreateImage(string checkCode)
{
int iwidth = (int)(checkCode.Length * 14);
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20);
Graphics g = Graphics.FromImage(image);
Font f = new System.Drawing.Font("Arial ", 10);//, System.Drawing.FontStyle.Bold);
Brush b = new System.Drawing.SolidBrush(Color.Black);
Brush r = new System.Drawing.SolidBrush(Color.FromArgb(166, 8, 8));
//g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),0,0,image.Width, image.Height);
// g.Clear(Color.AliceBlue);//背景色
g.Clear(System.Drawing.ColorTranslator.FromHtml("#99C1CB"));//背景色
char[] ch = checkCode.ToCharArray();
for (int i = 0; i < ch.Length; i++)
{
if (ch[i] >= '0' && ch[i] <= '9')
{
//数字用红色显示
g.DrawString(ch[i].ToString(), f, r, 3 + (i * 12), 3);
}
else
{ //字母用黑色显示
g.DrawString(ch[i].ToString(), f, b, 3 + (i * 12), 3);
}
}
//for循环用来生成一些随机的水平线
// Pen blackPen = new Pen(Color.Black, 0);
// Random rand = new Random();
// for (int i=0;i<5;i++)
// {
// int y = rand.Next(image.Height);
// g.DrawLine(blackPen,0,y,image.Width,y);
// }
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
//history back 不重复
Response.Cache.SetNoStore();//这一句
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
image.Dispose();
}
}
}