One account can only log on to one account at the same time (Single Point login)

Source: Internet
Author: User
Tags set cookie setcookie
Idea: 1: User Login-generates a random key (guid) (the server maintains a dictionary [ID, key] and stores it in memcache) -"also outputs the key to the client cookie. Each time the user accesses the page and checks whether the user logs on to the page, the user reads the cookie of the browser and determines whether the key is the same as that of the server. Eg: After a user logs on to chrome, the server generates a guid, the browser cookie stores a key, and the server memcache stores a key + usercode and guid ), after user a logs on to Firefox, the server generates a guid, And the browser cookie stores a key, and the server memcache stores a key + usercode and guid ), when user a refresh chrome again, the cookie of the browser is different from the value of memcache on the server (the value of memcache is checked by usercode), and the server is forced to exit.
1 /// <summary> 2 // memcache, define 3 /// </Summary> 4 Private Static cacheserver _ cacheserver = cacheserver. getcacheoperateinstance (); 5 6 /// <summary> 7 // set cookie 8 /// </Summary> 9 /// <Param name = "cookiename"> </param> 10/ // <Param name = "cookievalue"> </param> 11 // <Param name = "expires"> </param> 12 public static void setcookie (string cookiename, string cookievalue, datetime expires) 13 {14 httpcookie cookie = new httpcookie (cookiename) 15 {16 value = cookievalue, 17 expires = expires18}; 19 system. web. httpcontext. current. response. cookies. add (cookie ); 20} 21 22 /// <summary> 23 // after successful login, run 24 /// </Summary> 25 /// <Param name = "islogin"> </ param> 26 // <Param name = "pusercode"> </param> 27 // <Param name = "cookiedomain"> </param> 28 public void singlelogin (bool islogin, string pusercode, string cookiedomain) 29 {30 if (islogin) 31 {32 # region limits that the same account can only log on to 33 string singlekey = "singleguid" at the same time; 34 string singlevalue = guid. newguid (). tostring (); 35 setcookie (singlekey, cookiedomain, datetime. now. adddays (7); 36 _ cacheserver. addcacheruntime (singlekey + pusercode. tolower (), singlevalue, 1440*3); 37 # endregion38} 39}

Code in basecontrol:

1 /// <summary> 2 /// obtain the specified cookie value 3 /// </Summary> 4 /// <Param name = "cookiename"> cookiename </param> 5 /// <returns> </returns> 6 public static string getcookievalue (string cookiename) 7 {8 httpcookie cookie = system. web. httpcontext. current. request. cookies [cookiename]; 9 string STR = string. empty; 10 if (cookie! = NULL) 11 {12 STR = cookie. value; 13} 14 return STR; 15} 16 17 // <summary> 18 // name: Clear cookie19 /// </Summary> 20 private void removeallcookiesbase () 21 {22 foreach (string key in httpcontext. request. cookies. allkeys) 23 {24 httpcookie cookie = httpcontext. request. cookie [Key]; 25 cookie. domain = system. configuration. configurationmanager. appsettings ["cookiedomain"]; 26 cookie. expires = datetime. now. addday S (-1); 27 httpcontext. response. cookies. add (cookie ); 28} 29 30} 31 32 // <summary> 33 // code 34 in onactionexecuting in basecontrol /// </Summary> 35 // <Param name = "filtercontext "> </param> 36 protected override void onactionexecuting (actionexecutingcontext filtercontext) 37 {38 // your login object. Modify 39 usermodel _ USERPROFILE = new usermodel () by yourself (); 40 41 # region limits that the same account can only log on to one site at the same time. 42 string singlekey = "singleguid"; 43 // from client C Ookie value 44 var clientvalue = getcookievalue (singlekey); 45 46 string servervalue = string. empty; 47 // obtain 48 var servervalueobj = _ cacheserver from the server cache. getvaluewithcache (singlekey + _ USERPROFILE. usercode. tolower (); 49 If (servervalueobj! = NULL) 50 {51 servervalue = convert. tostring (servervalueobj); 52} 53 54 if (! Servervalue. equals (clientvalue) 55 {56 removeallcookiesbase (); 57 system. web. security. formsauthentication. signout (); 58 59 // login system address 60 string loginurl = system. configuration. configurationmanager. appsettings ["loginpath"]; 61 string hosturl = filtercontext. httpcontext. request. URL. host; 62 var https_hosturl = "http: //" + hosturl + "/"; 63 filtercontext. result = new redirectresult (loginurl + https_hosturl); 64}
Base. onactionexecuting (filtercontext); 65 # endregion66}

Modify the code according to the actual situation of your project.

 

  

One account can only log on to one account at the same time (Single Point login)

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.