实现方式,用户登录的时候记录用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(); //火狐浏览器。 } 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"); } } |