Single Sign On (SSO) is one of the most popular solutions for business integration. When developing an enterprise portal or e-commerce system, design a function that allows a user to log on only to the same website.
Cache objects are mainly used for Cache of Web applications. Each application needs to create an instance of the Cache object, and the Instance remains valid as long as the corresponding application domain remains active, all information about Cache object instances must be provided through the Cache attribute of the HttpContext object or the Cache attribute of the Page object.
In this example, the user's login information is saved in the Cache object and the expiration time is set to the time when the Session variable expires. Therefore, once the Session variable expires, the Cache object will expire; however, the Cache object can be accessed by all users. Therefore, it is easier to use it to save user information than to use the database.
The key code is as follows:
Code
1 protected void btnLogin_Click (object sender, EventArgs e)
2 {
3 int I = this. checkLogin (txtName. Text, txtPwd. Text );
4 if (I> 0)
5 {
6 string str_Key = txtName. Text + "_" + txtPwd. Text;
7 // obtain the value of the given str_Key in the Cache
8 string str_User = Convert. ToString (Cache [str_Key]);
9 // if there is no str_Key project in the Cache, the user is not logged on
10 if (str_User = String. Empty)
11 {
12 // TimeSpan constructor, used to determine whether to log on.
13 TimeSpan SessTimeOut = new TimeSpan (0, 0, HttpContext. Current. Session. Timeout, 0, 0 );
14 HttpContext. Current. Cache. Insert (str_Key, str_Key, null, DateTime. MaxValue, SessTimeOut, acheItemPriority. NotRemovable, null );
15 Session ["User"] = str_Key;
16 // The first logon is successful.
17 Response. Write ("
18}
19 else
20 {
21 // This user's record exists in the Cache. The table name has been logged on. You cannot log on again.
22 Response. Write ("
23 return;
24}
25}
26 else
27 {
28 Response. Write ("the user name or password is incorrect !!! ");
29}
30}