Users cannot log on to ASP. NET repeatedly.

Source: Internet
Author: User

Function Description: User A logs on with aid. User B also uses aid to log on without exiting User A. At this time, user a is denied.

Add the following code to global. asax:

Void application_acquirerequeststate (Object sender, eventargs e) {If (request ["mycookies"]! = NULL & session ["ID"]! = NULL) {httpcookie cookie = request. Cookies ["mycookies"]; If (cookie [session ["ID"]. tostring ()]! = NULL) {If (application [session ["ID"]. tostring ()]. tostring ()! = Cookie [session ["ID"]. tostring ()]. tostring () {timespan Ts = new timespan (-1, 0, 0, 0); cookie. expires = datetime. now. add (TS); response. appendcookie (cookie); Session ["ID"] = NULL; request. cookies. remove ("mycookies") ;}}} on the page for permission verification (wait for the incoming window)

 

Protected void button#click (Object sender, eventargs e) {If (request. Cookies ["mycookies"]! = NULL) {If (session ["ID"]! = NULL) {If (session ["ID"]. tostring ()! = This. textbox1.text) {res_cookie ();} else {response. Write ("Login successful! ") ;}} Else {res_cookie ();}}

Private void res_cookie () {string this_time = datetime. now. tostring (); application [this. textbox1.text] = this_time; httpcookie cookie = new httpcookie ("mycookies"); timespan Ts = new timespan (0, 0, 1, 0); cookie. values. add (this. textbox1.text, this_time); Session ["ID"] = This. textbox1.text; response. appendcookie (cookie); response. write ("Logon successful! ");} Add a test button:

Protected void button2_click (Object sender, eventargs e) {If (request. Cookies ["mycookies"] = NULL) {response. Write ("Logon Failed! ");} Else {response. Write (" Logon successful! ") ;}} Finally, release the data in the application in the session_end event.

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.