Implement single-point logon in ASP. NET (use cache to save user information in the server cache)

Source: Internet
Author: User
ArticleDirectory
    • Single Sign-On Test

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> <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312 "> <meta http-equiv =" author "content =" Chapter E "> <meta http-equiv =" website "content =" http://dotnet.aspx.cc/"> <br /> <style> H3 {Font: 17px} input {Font: 12px} span {Font: 12px} p {Font: 12px} H4 {Font: 12px} </style> <p> </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/>}

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.