ASP. NET limits repeated User Logon

Source: Internet
Author: User

 

 

This method records the session ID and logon time when a user logs on. Then write an XML file. The name of the user name indicates the time when the content is written to the user. After logon, each page compares the written files with the session logon time every 20 seconds. If it is correct, you do not need to worry about it. If it is incorrect, it is kicked out. Because the written file will not be updated if it is not logged on with the same user name.

CodeAllows you to log on to a single logon node for unlimited logon times. You can implement it with a slight modification.

Login Page

<% @ 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> No title page </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 = "button#click" text = "login"/> <br/>

</Div>

</Div>

</Form>

</Body>

</Html>

Login code

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 button#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

<% @ 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/javasOther ">

VaR x = 0;

Function myrefresh ()

{

Xrequest ();

VaR result = "";

XMLHTTP. Open ("Post", "test. aspx", false );

XMLHTTP. Send ("");

Result = XMLHTTP. responsetext;

If (result! = "OK ")

{

Alert ("sorry, your account is logged on elsewhere ");

Location. href = 'logost. aspx ';

}

X ++;

If (x <20)

{

SetTimeout ("myrefresh ()", 1000 );

}

}

Myrefresh ();

VaR XMLHTTP;

Function xrequest (){

If (window. XMLHttpRequest ){

XMLHTTP = new XMLHttpRequest (); // Firefox browser.

}

Else if (window. activexobject ){

XMLHTTP = new activexobject ("msxml2.xmlhttp"); // IE browser.

If (! XMLHTTP) {XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");}

}

}

</SCRIPT>

Code

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 (\" the directory is empty \ "); </SCRIPT> ");

}

Else

{

Foreach (filesysteminfo fitemp in DIS)

{

Response. Write (fitemp. Name + "<br> ");

}

}

}

}

The test. ASPX page is used to check whether the page is topped. You only need to write the code.

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 ();

}

}

Exit 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 ");

}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.