. NET implementation of single or several session expiration time setting simple scheme

Source: Internet
Author: User

. NET under the session is very simple, to write session value no type limit, set the session effective time is also very simple, directly set the session's Timeout property, type int, in minutes.

1             session["time"] = DateTime.Now.ToString ("yyyymmddhhmmss" ); 2             ;

However, to set multiple sessions with different effective times, it is less friendly. the valid time after setting overrides the previous value .

1             //writes the user login status to the session. 2session[" State"] =" on";3             //Setting the user logon status is valid for 1 days, unless the login clears the session, otherwise the full day logon status is valid. 4Session.Timeout = -* -;5 6             //set a session similar to the verification code, assuming that the validity time is 1 minutes. 7session[" Time"] = DateTime.Now.ToString ("YYYYMMDDHHMMSS");8Session.Timeout =1;

As above, two sessions are set, two sessions are valid for different periods, and the time to write the session will overwrite the previous write session validity time.

Easy to solve ideas

The build structure is:

1 tuple.create<string, object, int> (key, Value, time)

Tuple, which writes a tuple to the session. (in order to represent session name, write value, valid time)

1         Private voidSetSingleSession (stringKey,ObjectValueint?Timeout)2         {3             intTime = timeout??Session.Timeout;4             vartuple = tuple.create<string,Object,int>(key, value, time);5Session[key] =tuple;6}

However, there is a problem with this setting, and it does write values to the session, but does not implement the session function.

In other words, this session will not expire after the effective time!!! This is key, what to do? One key point to note is that you must not modify the timeout parameter of the session.

An alternative to consider here is that the expiration time is calculated based on the incoming valid time, the expiration time is saved, the session is judged to have exceeded the expiration deadline, the return is empty, and the session is emptied.

Overriding the SetSingleSession method

1         Private voidSetSingleSession (stringKeyObjectValueint?Timeout)2         {3             intTime = timeout??Session.Timeout;4DateTime EndTime = DateTime.Now +NewTimeSpan (0, Time,0);5             vartuple =tuple.create (key, value, endTime);6Session[key] =tuple;7}

Get Session Method

1         Private ObjectGetsinglesession (stringkey)2         {3             vartuple = Session[key] astuple<string,Object, datetime>;4             vardiff =datetime.compare (tuple. ITEM3, DateTime.Now);5             Objectresult =NULL;6             if(diff >0) result =tuple. item2;//normal return value before valid time cutoff7             ElseSession.remove (key);//Clear the Session after a valid time8             returnresult;9}

Just a basic program, followed by a better plan to re-update.

. NET implements a single or several session expiration time setting simple scheme

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.