ArticleDirectory
For some reason, a single user can only log on to one place in our application, which is also called Single Sign-On. Implementing Single Sign-On in ASP. NET is actually very simple. The following describes the main methods and allCodeFor analysis.
Implementation
By using the cache function, we store user login information in the cache and set the expiration time to the session expiration time. Therefore, once the session fails, our cache also expires; the cache can be accessed by all users. Therefore, it is easier to use it to save user information than to use the database.
View examples
Singlelogin. aspx code
<xmp> &lt;Meta http-equiv = &quot;Content-Type&quot; content = &quot;text/html; charset = gb2312 &quot;&gt; &lt;meta http-equiv =&quot; author &quot;content =&quot; Chapter E &quot;&gt; &lt;meta http-equiv =&quot; website &quot;content =&quot; http://dotnet.aspx.cc/&quot;&gt; &lt;br /&gt; &lt;style&gt; H3 {Font: 17px} input {Font: 12px} span {Font: 12px} p {Font: 12px} H4 {Font: 12px} &lt;/style&gt; &lt;p&gt; </xmp>
Singlelogin. aspx. CS code
Using system; <br/> using system. collections; <br/> using system. componentmodel; <br/> using system. data; <br/> using system. drawing; <br/> using system. web; <br/> using system. web. sessionstate; <br/> using system. web. ui; <br/> using system. web. UI. webcontrols; <br/> using system. web. UI. htmlcontrols; <br/> namespace emeng. exam <br/>{< br/> /// <br/> <summary> /// Summary of singlelogin. <Br/> // single-point logon <br/> // </Summary> <p> public class singlelogin: system. web. UI. page <br/> {<br/> protected system. web. UI. webcontrols. textbox username; <br/> protected system. web. UI. webcontrols. textbox password; <br/> protected system. web. UI. webcontrols. label MSG; <br/> protected system. web. UI. webcontrols. button login; <br/> private void page_load (Object sender, system. eventargs e) <br/>{< br/> // actual example can be accessed Q: <br/> // http://dotnet.aspx.cc/Exam/SingleLogin.aspx <br/>}< br/> # code generated by region web forms designer <br/> override protected void oninit (eventargs E) <br/>{< br/> initializecomponent (); <br/> base. oninit (E ); <br/>}< br/> /// <br/> <summary> // The method required by the designer. Do not use the code editor to modify the method. <br/> // /content of this method. <Br/> // </Summary> <p> private void initializecomponent () <br/>{< br/> This. login. click + = new system. eventhandler (this. login_click); <br/> This. load + = new system. eventhandler (this. page_load); <br/>}< br/> # endregion <br/> private void login_click (Object sender, system. eventargs e) <br/>{< br/> // the key that is uniquely identified. You can set rules as needed. <Br/> // used as a test. Here, the user name and password are combined for identification, and no other error checks are performed. <Br/> // generate key <br/> string skey = username. text + "_" + password. text; <br/> // obtain the value of the given key in the cache <br/> string suser = convert. tostring (Cache [skey]); <br/> // check for existence <br/> If (suser = NULL | suser = string. empty) <br/>{< br/> // The project where the key is not found in the cache. The table name user is not logged on, or the logon has timed out <br/> // note that the following method of using the timespan constructor to overload the version is the key to determining whether to log on. <Br/> timespan sesstimeout = new timespan (0, 0, system. web. httpcontext. current. session. timeout, 0, 0); <br/> httpcontext. current. cache. insert (skey, skey, null, datetime. maxvalue, sesstimeout, <br/> system. web. caching. cacheitempriority. notremovable, null); <br/> session ["user"] = skey; <br/> // you can do what you want when logging on for the first time. <Br/> MSG. Text = "</P> Hi! Welcome to [wonderful world of the mengxian meeting] "; <br/> MSG. Text + =". Have a good time! :) <P> "; <br/>}< br/> else <br/>{< br/> // records of this user are found in the cache, the table name has been logged on. You cannot log on again. <br/> MSG. TEXT = "</P> sorry, you seem to have logged on:-(<p>"; <br/> return; <br/>}< br/>}