关闭窗口后立即销毁Session并通过ADO层实现插入日志方法

来源:互联网 发布:超声波洗牙器 知乎 编辑:程序博客网 时间:2024/06/08 07:11

第一步:

新建日志表:B_SYS_LOG

if exists (select * from sysobjects where id = OBJECT_ID('[B_SYS_LOG]') and OBJECTPROPERTY(id, 'IsUserTable') = 1)
DROP TABLE [B_SYS_LOG]

CREATE TABLE [B_SYS_LOG] (
[ID] [int]  IDENTITY (1, 1)  NOT NULL,        //主键ID
[EmployeeName] [varchar]  (50) NULL,            //真实姓名
[HandleContent] [varchar]  (1000) NULL,         //操作内容
[ComputerName] [varchar]  (100) NULL,          //计算机名
[ComputerIP] [varchar]  (100) NULL,                //计算机IP地址
[HandleTime] [datetime]  NULL)                        //操作时间

 

第二步:

登录页面后台获取用户的Uid,UserName,EmployeeName,HandleContent,ComputerName,ComputerIP,代码如下在确认用户帐号密码正确后执行下面代码:

          Session["uid"] = uid;
          Session["username"] = this.Login1.UserName.Trim();
          DataSet ds=bbsu.GetList("UID=" + uid);
          Session["realname"] = ds.Tables[0].Rows[0]["RealName"].ToString();
          Session["computername"] = Dns.GetHostName();
          IPAddress[] ip = new IPAddress[1];
          ip = Dns.GetHostAddresses(Dns.GetHostName());
          Session["computerip"] = ip[0].ToString();

并调用 InsertLog("yhdlxt");方法来记录用户登录系统

 

第三步

第一种情况:主页面是个全部由框架搭建的页面,不包含<body>标签,示例如下:

主页面MainFramePage.aspx是个纯框架的页面,后台无关键代码,前台全部代码如下:

IE6中:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>耀莎进销存管理平台</title>
     <script type="text/javascript">
        function window.onbeforeunload()        //用户非法退出系统,也就是当用户关闭网页窗体时跳转到Close.aspx页面
        {
            window.location.href="Close.aspx";
        }
    </script>
</head>
<frameset id="mame1" rows="60,*,35" marginheight="0" marginwidth="0" frameborder="0">
    <frame id="TopFrame" width="100%" height="60" frameborder="0" src="Head.aspx" scrolling="no" marginheight="0" marginwidth="0"></frame>
    <frameset id="mame2" cols="150,*" marginheight="0" marginwidth="0" frameborder="0"> 
        <frame id="LeftFrame" src="LeftMenu.aspx" scrolling="no"></frame>
        <frame id="RightBottomFrame" name="RightBottomFrame" src="GridView.aspx" scrolling="no"></frame>
    </frameset>
    <frame id="BottomFrame" width="100%" height="35" frameborder="0" src="Feet.aspx" scrolling="no" marginheight="0" marginwidth="0"></frame>
</frameset>
</html>

IE7中:

<body onbeforeunload="window.open('Close.aspx')">

 

第二种情况:关闭一个普通的页面,带<body>标签,代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!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  language="javascript">

function ClosePage()
{
if((window.screenLeft>=10000 && window.screenTop>=10000)||event.altKey)
 {
   window.location.href="/ClosePage.aspx" //当用户关闭网页窗体时跳转到Close.aspx页面
  }
}

</script>
</head>
<body  onunload="ClosePage()">//onunload方法判断用户关闭窗口
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
    </form>
</body>
</html>

 

第四步:

Close.aspx页面前台代码什么都不用写,后台只要在Page_Load事件中操作即可

    protected void Page_Load(object sender, EventArgs e)
    {
        DbHelperSQL.InsertLog("yhtcxt");   //调用向系统插入日志方法,通过关键字“yhtcxt”告诉系统用户退出系统

        Session.Abandon();    //销毁Session

        Page.RegisterClientScriptBlock("message","<script>window.location.href = 'Default.aspx';</script>");
    }

 

第五步:

DbHelperSQL基类中有具体的插入日志的方法,代码如下:

 #region 用户操作日志

        ///<summary>
        ///用户操作日志
        ///</summary>
        public static void InsertLog(string SQLString)
        {
            DateTime HandleTime = TWXY_Function.GetDateTime();
            string sqlstring = SQLString.Trim().ToLower();
            string sql = sqlstring.Substring(0, 6);
            string a = "";
            string b = "";
            string HandleContent = "";
            switch (sql)
            {
                //case "select":
                //    if (sqlstring.Contains("where") == true)
                //    {
                //        a = sqlstring.Substring(sqlstring.IndexOf("from") + 4);
                //        b = a.Remove(a.IndexOf("where"));//获得了表名
                //    }
                //    else
                //    {
                //        b = sqlstring.Substring(sqlstring.IndexOf("from") + 4);//获得了表名
                //    }
                //    LogSql = "表" + b + "被用户执行“查询”操作,操作内容为:" + sqlstring.Replace("'", "’");
                //    break;
                case "yhdlxt":
                    HandleContent = "开始使用系统";
                    break;
                case "yhtcxt":
                    HandleContent = "已经退出系统";
                    break;
                case "insert":
                    a = sqlstring.Remove(sqlstring.IndexOf("("));
                    b = a.Substring(11);//获得了表名
                    HandleContent = "表" + b + "被用户执行“插入”操作,操作内容为:" + sqlstring.Replace("'", "’");
                    break;
                case "update":
                    a = sqlstring.Remove(sqlstring.IndexOf("set"));
                    b = a.Substring(7);//获得了表名
                    HandleContent = "表" + b + "被用户执行“编辑”操作,操作内容为:" + sqlstring.Replace("'", "’");
                    break;
                case "delete":
                    a = sqlstring.Remove(sqlstring.IndexOf("where"));
                    b = a.Substring(7);//获得了表名
                    HandleContent = "表" + b + "被用户执行“删除”操作,操作内容为:" + sqlstring.Replace("'", "’");
                    break;
                default:
                    break;
            }
            string EmployeeName = "";

            //System.Web.HttpContext.Current.Session["Session名称"]是类中获取Session的方法
            if (System.Web.HttpContext.Current.Session["employeename"] != null)    

            {
                EmployeeName = System.Web.HttpContext.Current.Session["employeename"].ToString();
            }
            else
            {
                EmployeeName = "非使用用户";
            }
            string ComputerName = "";
            if (System.Web.HttpContext.Current.Session["computername"] != null)
            {
                ComputerName = "计算机名:" + System.Web.HttpContext.Current.Session["computername"].ToString();
            }
            else
            {
                ComputerName = "计算机名:";
            }
            string ComputerIP = "";
            if (System.Web.HttpContext.Current.Session["computerip"] != null)
            {
                ComputerIP = "计算机IP:" + System.Web.HttpContext.Current.Session["computerip"].ToString();
            }
            else
            {
                ComputerIP = "计算机IP:";
            }
            string insertlog = "insert into B_SYS_LOG (EmployeeName,HandleContent,ComputerName,ComputerIP,HandleTime)values('" + EmployeeName + "','" + HandleContent + "','" + ComputerName + "','" + ComputerIP + "','" + HandleTime + "')";
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand(insertlog, connection))
                {
                    try
                    {
                        connection.Open();
                        cmd.ExecuteNonQuery();
                    }
                    catch (System.Data.SqlClient.SqlException E)
                    {
                        connection.Close();
                        throw new Exception(E.Message);
                    }
                }
            }
        }
        #endregion