login.css
body{ background:url(../images/login/loginbg.png) repeat-x }
.logo{ height:35px; width:785px; margin:60px auto 45px auto; clear:both}
.logo img{ float:left;}
.loginmain{ width:785px; margin:0 auto; height:275px;}
.logintext{ width:450px; height:262px; float:left; background:url(../images/login/loginlbg.png) no-repeat; margin-top:8px;}
.lgtext_con{ width:410px; height:auto; margin:70px auto 0 auto;}
.lgcon_tab{ width:100%; margin-top:10px; color:#333}
.lgcon_tab th{ line-height:30px; text-align:right; font-size:14px; width:75px;}
.lgcon_tab td{ line-height:30px; text-align:left; font-size:14px;}
.logincen{ height:273px; width:53px; background:url(../images/login/loginxbg.png) repeat-y; float:left;}
.logindl{ height:273px; width:279px; background:url(../images/login/logindlbg.png) no-repeat; float:right;}
.lgdl_tab{ width:216px; margin:65px auto 0 auto;}
.lgdl_tab th{ width:55px; text-align:right; font-weight:normal; font-size:14px; color:#343434; line-height:35px;}
.lgdl_tab td{ text-align:left; line-height:35px;}
.lgdl_tab input.wbc{ width:146px; height:20px; border:#A1B9C5 solid 1px;}
.lgdl_tab input.wbd{ width:45px; height:20px; border:#A1B9C5 solid 1px;}
.lgdlbut{ width:86px; height:32px; float:left; margin-left:43px; clear:both;}
input.dlbuta{ width:86px; height:32px; background:url(../images/login/dlan.png); border:none; text-align:left;}
input.dlbutb{ width:86px; height:32px; background:url(../images/login/dlanb.gif); border:none; text-align:left;}
master.css
/* 全局CSS定义 */
li,ul,dt,form,ol,FIELDSET,p,h1,h2,h3,h4,h5,TABLE,IMG,dl,dt,dd{margin: 0px;padding: 0px;border: 0px; list-style:none}
body{ margin:0; padding:0; text-align:center; font-size:12px;}
A:link,A:visited{font:"宋体";color:#1c1c1c;text-decoration: none;}
A:hover,A:active{text-decoration: underline; color:#000000}
/* 通用空白间距 */
.h2{ height:2px; font-size:1px; line-height:0px; clear:both; overflow:hidden; margin:0 auto; }
.h10{ height:10px; font-size:1px; line-height:0px; clear:both; overflow:hidden; margin:0 auto;}
.h5{ height:5px; font-size:1px; line-height:0px; clear:both; overflow:hidden; margin:0 auto;}
.h15{ height:15px; font-size:1px; line-height:0px; clear:both; overflow:hidden; margin:0 auto;}
.h20{ height:20px; font-size:1px; line-height:0px; clear:both; overflow:hidden; margin:0 auto;}
验证码的页面:
页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ValidateCode.aspx.cs" Inherits="SHIB3.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>
后台:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Drawing;
namespace SHIB3
{
public partial class ValidateCode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.CreateCheckCodeImage(GenerateCheckCode());
}
private string GenerateCheckCode()
{
int number;
char code;
string checkCode = String.Empty;
System.Random random = new Random();
for (int i = 0; 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();
}
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();
}
}
}
}
页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="login.aspx.cs" Inherits="SHIB3.Account.login1" %>
<%@ Register Assembly="ComponentArt.Web.UI" Namespace="ComponentArt.Web.UI" TagPrefix="ComponentArt" %>
<!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">
<link href="../App_Themes/Default/Styles/login.css" rel="stylesheet" type="text/css" />
<link href="../App_Themes/Default/Styles/master.css" rel="stylesheet" type="text/css" />
<title>竞价平台登录</title>
<script src="../Scripts/validate.js" type="text/javascript"></script>
<script src="../Scripts/WMI.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
//验证
function validate() {
var tb_user = document.getElementById("tb_user");
var tb_password = document.getElementById("tb_password");
var tb_validate = document.getElementById("tb_validate");
if (!checkInputValueIsEmpty(tb_user)) {
alert("请输入用户名!");
tb_user.focus();
return false;
}
if (!checkInputValueIsEmpty(tb_password)) {
alert("请输入密码!");
tb_password.focus();
return false;
}
if (!checkInputValueIsEmpty(tb_validate)) {
alert("请输入验证码!");
tb_validate.focus();
return false;
}
return true;
}
//登录
function login() {
if (validate()) {
callback_login.callback("login");
}
}
//改变验证码
function changeCode() {
var imgNode = document.getElementById("imgCode");
imgNode.src = "../ValidateCode.aspx?t=" + (new Date()).valueOf();
}
//取得焦点
function getFocus(control) {
document.getElementById(control).focus();
}
//回车键事件:用document.onkeydown = function(),那么按了回车键会自动调用
document.onkeydown = function() {
if (event.keyCode == 13) {
document.getElementById("img_login").click(); //点击回车键调用button的点击事件
event.returnValue = false; //取消回车键的默认操作
}
}
function loginload() {
document.getElementById("img_login").className = "dlbutb";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div class="logindl">
<table border="0" cellspacing="0" cellpadding="0" class="lgdl_tab">
<tr>
<th>
用户名:
</th>
<td colspan="2">
<asp:TextBox οnfοcus="this.select();" οnblur="DBC2SBC(this);" ID="tb_user" runat="server"
CssClass="wbc" MaxLength="20" />
</td>
</tr>
<tr>
<th>
密 码:
</th>
<td colspan="2">
<asp:TextBox οnfοcus="this.select();" οnblur="DBC2SBC(this);" ID="tb_password" runat="server"
CssClass="wbc" TextMode="Password" MaxLength="30" />
</td>
</tr>
<tr>
<th>
验证码:
</th>
<td style="width: 110px;">
<label>
<asp:TextBox οnfοcus="this.select();" οnblur="DBC2SBC(this);" ID="tb_validate" runat="server"
CssClass="wbd" MaxLength="4" />
<iframe id="imgCode" src="../ValidateCode.aspx" width="50px" height="20px" scrolling="no"
frameborder="0" marginheight="0" marginwidth="0"></iframe>
</label>
</td>
<td>
<a href="javascript:changeCode();" class="changePicture">换一张</a>
</td>
</tr>
<tr>
<td>
</td>
<td colspan="2" style="color: #0B4172">
<asp:CheckBox ID="cb_rememberPassoword" runat="server" Text="记住密码" CssClass="jzmm" />
</td>
</tr>
<tr>
<td>
</td>
<td colspan="2">
<ComponentArt:CallBack ID="callback_login" runat="server" OnCallback="callback_login_Callback"
PostState="True">
<Content>
<asp:PlaceHolder ID="pl_btn" runat="server">
<input id="img_login" type="button" class="dlbuta" style="cursor: pointer" οnclick="login();" />
</asp:PlaceHolder>
</Content>
<ClientEvents>
<BeforeCallback EventHandler="loginload" />
</ClientEvents>
</ComponentArt:CallBack>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
后台:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;
using System.Collections.Generic;
using YC.DB.DataSource;
using JahaSoft.Security;//密码
using SHIB3.Class;
using SHIB3.DataAccess.Account;//实体类的引用
namespace SHIB3.Account
{
public partial class login1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack && !callback_login.IsCallback)
{
getUserByCook();
}
}
/// <summary>
/// 从cook中取得值:user、password
/// </summary>
private void getUserByCook()
{
HttpCookie cokname = Request.Cookies.Get("actionSysUser");//每个项目的cookName不能相同,否则登录是另外一个用户,不是自己填写的
if (cokname != null)
{
tb_user.Text = Server.UrlDecode(cokname.Value);
}
HttpCookie cokpass = Request.Cookies.Get("actionPassword");
if (cokpass != null)
{
tb_password.Attributes.Add("value", Server.UrlDecode(cokpass.Value));
}
if (cokname != null && cokpass != null)
{
cb_rememberPassoword.Checked = true;
}
Session["actionSysUser"] = null;
}
//保存值到cookies中
private void SaveCookies(string key, string value)
{
HttpCookie cokpass = Request.Cookies.Get(key);
if (cokpass == null)
{
cokpass = new HttpCookie(key);
cokpass.Expires = DateTime.Now.AddDays(10);
cokpass.Value = value;
Response.Cookies.Add(cokpass);
}
else
{
cokpass.Value = value;
cokpass.Expires = DateTime.Now.AddDays(10);
Response.Cookies.Add(cokpass);
}
}
//设置cookies过期
private void RemoveCookies(string key)
{
HttpCookie cokpass = Request.Cookies.Get(key);
if (cokpass != null)
{
cokpass.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(cokpass);
}
}
/// <summary>
/// 设置cook的值
/// </summary>
private void setUserByCook()
{
if (cb_rememberPassoword.Checked)
{
SaveCookies("actionSysUser", Server.UrlEncode(tb_user.Text));
SaveCookies("actionPassword", Server.UrlEncode(tb_password.Text));
}
else
{
RemoveCookies("actionSysUser");
RemoveCookies("actionPassword");
}
}
/// <summary>
/// 登录判断
/// </summary>
/// <param name="uID"></param>
/// <param name="pwd"></param>
/// <returns></returns>
private int userLogin()
{
int result = 0;
string filterStr = string.Format(VIEW_竞价用户.Col_用户名 + "='{0}'", tb_user.Text);
ObjModelDataSource<VIEW_竞价用户> source = new ObjModelDataSource<VIEW_竞价用户>(filterStr);
if (source.DataObjectSource == null)
{
return 1;//数据库连接失败
}
if (source.Count == 0)
{
return 2;//用户名不存在
}
VIEW_竞价用户 obj = source[0] as VIEW_竞价用户;
if (obj == null)
{
return 2;//用户名不存在
}
if (Crypto.Encrypt(tb_password.Text.Trim()) != obj.密码)
{
return 3;//密码不正确
}
if (obj.允许登录 == 0m)
{
return 4;//用户被禁止登录
}
if (obj.ID != "0")
{
if ((string.IsNullOrEmpty(obj.调度员) && obj.调度员 == "0") || (string.IsNullOrEmpty(obj.信息员) && obj.信息员 == "0"))
{
return 5;//用户未授权
}
}
if (Session["CheckCode"] == null)
{
return 6;
}
if (String.Compare(Session["CheckCode"].ToString(), tb_validate.Text, true) != 0)
{
return 7;
}
setUserByCook();
getUserInfo(obj);
return result;
}
protected ActionSysUser LoginUser
{
get
{
return Session["actionSysUser"] as ActionSysUser;
}
}
/// <summary>
/// 需要存储的用户session信息
/// </summary>
/// <param name="obj"></param>
private void getUserInfo(VIEW_竞价用户 obj)
{
ActionSysUser actionSysUser = new ActionSysUser();
if (obj.ID == "0")//系统管理员
{
actionSysUser.UserName = "系统管理员";
actionSysUser.UserBranch = "0";
actionSysUser.BranchName = "";
}
else
{
actionSysUser.UserName = obj.姓名;
actionSysUser.BranchName = obj.公司名称;
actionSysUser.UserBranch = obj.公司ID;
}
actionSysUser.LoginID = obj.ID;
actionSysUser.UserID = obj.ID;
actionSysUser.LoginName = obj.用户名;
actionSysUser.信息员 = obj.信息员;
actionSysUser.调度员 = obj.调度员;
actionSysUser.Pwd = JahaSoft.Security.Crypto.Encrypt(tb_password.Text);
actionSysUser.LoginTime = DateTime.Now;
Session["actionSysUser"] = actionSysUser;
}
//操作的callback
protected void callback_login_Callback(object sender, ComponentArt.Web.UI.CallBackEventArgs e)
{
string js = string.Empty;
string[] parameter = e.Parameters;
switch (parameter[0])
{
case "login":
switch (userLogin())
{
case 0:
js = "<script>window.location='../loadingpage.aspx?nextpage=Index.aspx';</script>";
break;
case 1:
js = "<script>alert('数据库连接失败!');</script>";
break;
case 2:
js = "<script>alert('用户名不存在!');getFocus('tb_user');</script>";
break;
case 3:
js = "<script>alert('密码不正确!');getFocus('tb_password');</script>";
break;
case 4:
js = "<script>alert('用户被禁止登录!');</script>";
break;
case 5:
js = "<script>alert('用户未授权!');</script>";
break;
case 6:
js = "<script>alertalert('未可知错误!');getFocus('tb_validate');</script>";
break;
case 7:
js = "<script>alert('验证码不匹配!');getFocus('tb_validate');</script>";
break;
default:
js = "<script>alert('登录失败!');</script>";
break;
}
break;
default:
break;
}
pl_btn.RenderControl(e.Output);
if (!string.IsNullOrEmpty(js))
{
e.Output.Write(js);
}
}
}
}
session信息
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace SHIB3.Class
{
public class ActionSysUser
{
private string _loginID;
public string LoginID
{
get { return _loginID; }
set { _loginID = value; }
}
private string _userID;
/// <summary>
/// 人员编号
/// </summary>
public string UserID
{
get { return _userID; }
set { _userID = value; }
}
private string _loginName;
/// <summary>
/// 名称
/// </summary>
public string LoginName
{
get { return _loginName; }
set { _loginName = value; }
}
private string _pwd;
/// <summary>
/// 密码
/// </summary>
public string Pwd
{
get { return _pwd; }
set { _pwd = value; }
}
private string _userName;
public string UserName
{
get { return _userName; }
set { _userName = value; }
}
private string _userpCmd;
public string UserpCmd
{
get { return _userpCmd; }
set { _userpCmd = value; }
}
private string _userBranch = "0";
/// <summary>
/// 角色所属分公司:编号
/// </summary>
public string UserBranch
{
get { return _userBranch; }
set { _userBranch = value; }
}
private string _branchName;
/// <summary>
/// 角色所属分公司:名称
/// </summary>
public string BranchName
{
get { return _branchName; }
set { _branchName = value; }
}
private string _信息员;
/// <summary>
/// 系统角色: 信息员
/// </summary>
public string 信息员
{
get { return _信息员; }
set { _信息员 = value; }
}
private string _调度员;
/// <summary>
/// 系统角色: 调度员
/// </summary>
public string 调度员
{
get { return _调度员; }
set { _调度员 = value; }
}
private DateTime _loginTime;
/// <summary>
/// 角色所属分公司:名称
/// </summary>
public DateTime LoginTime
{
get { return _loginTime; }
set { _loginTime = value; }
}
private string _ruleXml;
public string RuleXml
{
get { return _ruleXml; }
set { _ruleXml = value; }
}
private decimal _listalign = 0m;
public decimal Listalign
{
get { return _listalign; }
set { _listalign = value; }
}
private XmlDocument _doc = null;
protected XmlDocument Doc
{
get
{
if (_doc == null && !string.IsNullOrEmpty(_ruleXml))
{
_doc = new XmlDocument();
_doc.LoadXml(RuleXml);
}
return _doc;
}
}
private string _DefaultCode = string.Empty;
private string _DefaultUrl = string.Empty;
private string _DefaultLeftCode = string.Empty;
public string DefaultLeftCode
{
get { return _DefaultLeftCode; }
set { _DefaultLeftCode = value; }
}
public string DefaultUrl
{
get
{
return _DefaultUrl;
}
set { _DefaultUrl = value; }
}
/// <summary>
/// 默认页面
/// </summary>
/// <returns></returns>
public string DefaultCode
{
get { return _DefaultCode; }
set { _DefaultCode = value; }
}
/// <summary>
/// 验证模块权限
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
public bool ValidataModule(string code)
{
if (Doc == null)
{
return false;
}
string xpath = string.Format("/role/role[@code=\"{0}\"]", code);
XmlNode node = Doc.DocumentElement.SelectSingleNode(xpath);
if (node == null)
{
return false;
}
return true;
}
/// <summary>
/// 验证子模块权限
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
public bool ValidataChildModule(string code)
{
if (Doc == null)
{
return false;
}
string xpath = string.Format("/role/role/role[@code=\"{0}\"]", code);
XmlNode node = Doc.DocumentElement.SelectSingleNode(xpath);
if (node == null)
{
return false;
}
return true;
}
/// <summary>
/// 验证页面权限
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
public bool ValidataFunctionPoint(string code)
{
if (Doc == null)
{
return false;
}
string xpath = string.Format("/role/role/role/role[@code=\"{0}\"]", code);
XmlNode node = Doc.DocumentElement.SelectSingleNode(xpath);
if (node == null)
{
return false;
}
return true;
}
/// <summary>
/// 验证操作权限
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
public bool ValidataCmd(string code)
{
if (Doc == null)
{
return false;
}
string xpath = string.Format("/role/role/role/role/role[@code=\"{0}\"]", code);
XmlNode node = Doc.DocumentElement.SelectSingleNode(xpath);
if (node == null)
{
return false;
}
return true;
}
}
}