ASP.NET 限制使用者重複登入

來源:互聯網
上載者:User

 

 

實現方式,使用者登入的時候記錄用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>

        &nbsp;<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");

    }

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.