實現方式,使用者登入的時候記錄用Session他的ID和登入時間。然後寫入一個XML檔案。以他的使用者名稱命名,內容寫入他的登入時間。登入後每個頁面每20秒用Session的登入時間去比對寫入的檔案。如果正確則不用理會 如果不正確則被被踢出。因為寫入的檔案如果不是同一使用者名稱登入的話是不會更新的。 代碼可以實現 使用者單一登入 單點登入 無限人次登入 。稍微修改就可以實現了。 login 頁面 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="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> </head> <body> <form id="form1" runat="server"> <div> <div> <asp:TextBox ID="txtUser" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="login" /><br /> </div> </div> </form> </body> </html> login 代碼 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 login : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { string strUserId = txtUser.Text; string times = DateTime.Now.ToString("yyyyMMddHHmmss"); string body = CourseXMLFileHeader + "<userTime>" + times + "</userTime></NewDataSet>"; string CourseFilePath =Server.MapPath(".") + "file://userlist//" + strUserId + ".XML"; if (!System.IO.File.Exists(CourseFilePath)) { System.IO.FileStream NewText = System.IO.File.Create(CourseFilePath); NewText.Close(); Session["SESSION_USERTIME"] = times; Session["SESSION_USERName"] = strUserId; } else { System.IO.File.Delete(CourseFilePath); System.IO.FileStream NewText = System.IO.File.Create(CourseFilePath); NewText.Close(); Session["SESSION_USERTIME"] = times; Session["SESSION_USERName"] = strUserId; } System.IO.StreamWriter sw = new System.IO.StreamWriter(CourseFilePath, true); body = body.Replace("\'", "\""); sw.WriteLine(body); sw.Close(); Response.Redirect("Default.aspx"); } public String CourseXMLFileHeader = @"<?xml version='1.0' standalone='yes'?><NewDataSet>"; } default 頁面 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!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 id="Head1" runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <asp:GridView ID="DataList1" runat="server"> </asp:GridView> </form> </body> </html> <script type="text/javascript"> var x=0; function myRefresh() { xRequest(); var result = ""; xmlHttp.open("Post", "test.aspx", false); xmlHttp.send(""); result = xmlHttp.responseText; if(result!="ok") { alert("sorry,您的帳號在別處登入"); location.href='logost.aspx'; } x++; if(x<20) { setTimeout("myRefresh()",20*1000); } } myRefresh(); var xmlHttp; function xRequest(){ if(window.XMLHttpRequest){ xmlHttp = new XMLHttpRequest(); //Firefox瀏覽器。 } else if(window.ActiveXObject){ xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); //IE瀏覽器。 if(!xmlHttp){xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");} } } </script> 代碼 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.IO; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { DirectoryInfo di = new DirectoryInfo(Server.MapPath(".") + "file://userlist//"); FileSystemInfo[] dis = di.GetFileSystemInfos(); if (dis.Length < 1) { Response.Write("<script>alert(\"目錄是空的\");</script>"); } else { foreach (FileSystemInfo fitemp in dis) { Response.Write(fitemp.Name + "<br>"); } } } } Test.aspx 這個頁面是用來檢查是否被頂 只要寫代碼就可以了 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.IO; using System.Xml; public partial class test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string times = Session["SESSION_USERTIME"].ToString(); string userid= Session["SESSION_USERName"].ToString(); string CourseFilePath = Server.MapPath(".") + "file://userlist//" + userid + ".XML"; if (times.Equals(ReadXmlReturnNode(CourseFilePath, "userTime"))) { this.Response.Write("ok"); this.Response.End(); } else { this.Response.Write("no"); this.Response.End(); } } public static string ReadXmlReturnNode(string XmlPath, string Node) { XmlDocument docXml = new XmlDocument(); docXml.Load(@XmlPath); XmlNodeList xn = docXml.GetElementsByTagName(Node); return xn.Item(0).InnerText.ToString(); } } 退出介面 logost .aspx 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 logost : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Session.Remove("SESSION_USERTIME"); Session.Remove("SESSION_USERName"); Response.Redirect("login.aspx"); } } |