Asp.Net 使用者登出紀錄

Asp.Net 使用者登出紀錄

這篇文是半夜三點睡不著覺 逛 憂鬱小舖時看到有人在問 如何做關閉IE視窗問題(使用者登出)。

小弟我就上網找到眾多高手的心得分享…然後實作出來,參考了91大大、保哥的文。

 

網站的架構(隨便拉兩頁跟一個jquery):

image

 

Default.aspx:



<html>
<head runat="server">
    <title></title>
    <script src="js/jquery.js" type="text/javascript"></script>
    <script type="text/javascript" language="javascript">
        window.onbeforeunload = function () {
            return "是否確定離開本頁面?"
        };

        window.onunload = function logoutUser() {
            $.get("Default2.aspx");
        }

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <%--如果是在應用程式中的頁面轉換不需要詢問是否離開加入下列的js--%>
        <input type="submit" value="送出" onclick="window.document.body.onbeforeunload=null;return true;" />
    </div>
    </form>
</body>
</html>

Default.aspx.cs:


using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Session.Add("id", "sam");
    }
}

Default2.aspx:



<!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>

Default2.aspx.cs:


using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Diagnostics;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        UserLogOut_Act();
    }

    /// <summary>
    /// 我是存到event log中 請自由發揮 ^_^
    /// </summary>
    private void UserLogOut_Act()
    {
        string eventLog = "SamTest Message";
        string eventSource = "SamTest WebSite";

        if (!EventLog.SourceExists(eventSource))
        {
            EventLog.CreateEventSource(eventSource, eventLog);
        }

        EventLog myLog = new EventLog(eventLog);
        myLog.Source = eventSource;

        myLog.WriteEntry(string.Format("User Logout information in the Web application {0} {1} userID:{2} 已於{3}離線",
            eventSource, "\r\n\r\n", Session["id"].ToString(), DateTime.Now.ToString("yyyy/MM/dd HH:mm:sss")),
            EventLogEntryType.Information);
    }
}

 

執行畫面:

image

 

請關掉網頁哦…不要按送出內,然後就會跳出詢問是否離開此網頁。

image

 

點擊 離開此頁 後 會觸發此事件:


            $.get("Default2.aspx");
        }

 

Default2.aspx.cs 的程式就run起來了…


    {
        UserLogOut_Act();
    }

    /// <summary>
    /// 我是存到event log中 請自由發揮 ^_^
    /// </summary>
    private void UserLogOut_Act()
    {
        string eventLog = "SamTest Message";
        string eventSource = "SamTest WebSite";

        if (!EventLog.SourceExists(eventSource))
        {
            EventLog.CreateEventSource(eventSource, eventLog);
        }

        EventLog myLog = new EventLog(eventLog);
        myLog.Source = eventSource;

        myLog.WriteEntry(string.Format("User Logout information in the Web application {0} {1} userID:{2} 已於{3}離線",
            eventSource, "\r\n\r\n", Session["id"].ToString(), DateTime.Now.ToString("yyyy/MM/dd HH:mm:sss")),
            EventLogEntryType.Information);
    }

執行結果:來去事件檢視器中看吧…

image

 

感覺文法跟文筆有點問題(其實是腦子有問題??)…xd,請不嗇指教…(罵太兇的我就刪刪刪,嘿嘿嘿),嗯~just kidding,希望大大們多指教。