Single Sign-On: If a B C is used to log on to any of the three websites, no logon is required for other websites.
My Implementation ideas:
Cookie is used. Because the cookie exists on the client, all three websites of a B and C can access it.
The architecture is as follows:
AddCookie: Save Cookie
LoginOutGo: delete Cookie
Defualt: Home Page
Login: logon page
LoginOut: log out
The wegconfig configuration is as follows:
<Deleetask> <! -- Account password --> <add key = "acc" value = "51 aspx"/> <add key = "pas" value = "51 aspx"/> <! -- Server certificate expiration time (minutes) --> <add key = "timeout" value = "30"/> <add key = "SignSite" value = "http: // localhost: 49840/Public/AddCookie. aspx; http: // localhost: 50043/Public/AddCookie. aspx; http: // localhost: 50274/Public/AddCookie. aspx; "/> <add key =" SignSiteOut "value =" http: // localhost: 49840/Public/LoginOutGo. aspx, http: // localhost: 50043/Public/LoginOutGo. aspx, http: // localhost: 50274/Public/LoginOutGo. aspx; "/> </appSettings>
<authentication mode="Forms"> <forms loginUrl="/Login.aspx" name=".WebSite" protection="All" slidingExpiration="true" timeout="4320" path="/" defaultUrl="/default.aspx"></forms> </authentication>
Logon Process:
Enter the account and password: Both 51 aspx
Click Submit. Click Submit. The event is triggered:
Protected void btnSubmit_Click (object sender, EventArgs e) {Uri baseUri = new Uri (Request. url. absoluteUri. toString (); Uri absoluteUri = new Uri (baseUri, "/default. aspx "); // Response. write (absoluteUri. toString (); string fromurl = new Uri (baseUri, FormsAuthentication. defaultUrl ). toString (); // The starting URL path if (string. isNullOrEmpty (fromurl) fromurl = absoluteUri. toString (); string next = allLoginUrl ;/ /Touch the user login verification (the account and password are on the web. config) // the real environment here should be verified through the database if (this.txt Account. text = System. configuration. configurationManager. appSettings ["acc"] & this.txt Passport. text = System. configuration. configurationManager. deleetask[ "pas"]) {FormsAuthenticationTicket tk = new FormsAuthenticationTicket (1, this.txt Account. text, System. dateTime. now, DateTime. now. addMinutes (double. parse (System. configuration. config UrationManager. appSettings ["timeout"]), false, "test user data"); string key = FormsAuthentication. encrypt (tk); // The encrypted ticket string url = next. split (';') [0]; // Split the next page to be redirected from the URL. next = next. replace (url + ";", ""); // string to be redirected in the next round // Response. redirect (from); Response. redirect (url + "? CookieTicket = "+ key +" & FromUrl = "+ fromurl +" & NextUrl = "+ next ); // jump to the next page /// generate the token // string tokenValue = this. getGuidString (); // HttpCookie tokenCookie = new HttpCookie ("Token"); // tokenCookie. values. add ("Value", tokenValue); // tokenCookie. domain = "passport.com"; // Response. appendCookie (tokenCookie); // generate the master site credential // object info = true; // CacheManager. tokenInsert (tokenValue, info, DateTime. now. AddMinutes (double. parse (System. configuration. configurationManager. appSettings ["timeout"]); // jump back to the substation // if (Request. queryString ["BackURL"]! = Null) // Response. Redirect (Server. UrlDecode (Request. QueryString ["BackURL"]);} else {Response. Write ("sorry, the account or password is incorrect! Configure the account password in Web. config! ");}}
The URL of the jump link is http: // localhost: 49840/Public/AddCookie. aspx. The parameter is omitted. The Page_Load event of AddCookie is triggered.
Public partial class AddCookie: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {string from = Request ["FromUrl"]; // The starting URL path string next = Request ["NextUrl"]; // The URL string key = Request ["CookieTicket"] to be redirected; // The encrypted Cookie text if (string. isNullOrEmpty (from) Response. redirect (FormsAuthentication. defaultUrl); if (key! = Null & key! = "") {System. web. httpCookie ck = new HttpCookie (FormsAuthentication. formsCookieName, key); ck. path = FormsAuthentication. formsCookiePath; ck. expires = System. dateTime. now. addYears (10); Response. cookies. add (ck); // Add the passed encrypted authentication ticket to the room Session ["UserName"] = FormsAuthentication. decrypt (ck. value. toString ()). name; string url = next. split (';') [0]; // Split the next page to be redirected from the URL. next = next. replace (url + ";"," "); // The string if (url! = "") {Response. Redirect (url + "? CookieTicket = "+ key +" & FromUrl = "+ from +" & NextUrl = "+ next);} else // no more pages are available for redirect to {Response. redirect (from); // return to the starting page }}}}
Jump URL: http: // localhost: 50043/Public/AddCookie. aspx
In this way, the url is redirected to the AddCookie of the third website. When the url is empty, the parameter is skipped and the original page is displayed, that is, the default page of the first website.
Cancellation process:
Directly clear the cookie.
About: AuthBase. cs [verify logon or not]
namespace SSO.Public{ public class AuthBase { public bool CheckLogin() { bool flg = true; HttpCookie cookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName.ToString()]; //FormsAuthenticationTicket tk = (FormsAuthenticationTicket)(cookie.Value); if (cookie == null) flg= false; else { string name = FormsAuthentication.Decrypt(cookie.Value.ToString()).Name; if (name != "51aspx") flg= false; } return flg; } }
Source code download: http://pan.baidu.com/netdisk/singlepublic? Fid = 1079743_1871944408