Code for assigning values to sessions and obtaining session values in asp.net

Source: Internet
Author: User

Asp tutorial. the definitions and cancellation of sessions in net are sometimes scattered. When everyone in the Working Group defines sessions differently and the names are random, a session is managed in a unified manner to facilitate the standardization of sessions.

// Interface for session
Public interface isession {
String tostring ();
}


2. session

// Managerinfo is a class in the model and inherits directly
// When an object is put into the session, the object must be serializable.

[Serializable]
Public class loginsession: managerinfo, isession
{
Public loginsession (): base ()
{
Sessionid = "";
}

Public string sessionid {get; set ;}

Public override int gethashcode ()
{
Return sessionid. gethashcode ();
}

Public override string tostring ()
{
If (! String. isnullorempty (sessionid ))
Return sessionid;
Else
Return "";
}
}


Session assignment

Loginsession currentmanager = new loginsession ();
Currentmanager. username = "admin ";
Currentmanager. permission = "all ";
Currentmanager. sessionid = httpcontext. current. session. sessionid; <br> httpcontext. current. session [apps tutorial etting. globalsessionname] = currentmanager;
Httpcontext. current. session. timeout = 200;


Get the session Value

Public static t getglobalsessionvalue <t> (string _ propertyname)
{
Return getsessionvalue <t> (common. const. deleetting. globalsessionname, _ propertyname );
}

Public static t getsessionvalue <t> (string _ sessionname, string _ propertyname)
{
T retval = default (t );
String propertyname = _ propertyname. tolower ();

If (convert. tostring (httpcontext. current. session [_ sessionname])! = "")
{
Object sessionobject = (object) httpcontext. current. session [_ sessionname];

If (sessionobject is isession)
{
Propertyinfo [] propertyinfos = sessionobject. gettype (). getproperties ();

Foreach (var pi in propertyinfos)
{
String refname = pi. name. tolower ();
If (propertyname = refname)
{
Retval = (t) pi. getvalue (sessionobject, null );
Break;
}
}
}
}

Return retval;
}

In the program, you can obtain the value of a certain item in the session.

String _ tmpstr = utilities. getglobalsessionvalue <string> ("username ");
Int _ tmpint = utilities. getglobalsessionvalue <int> ("pagesize ");
Model. manager = utilities. getglobalsessionvalue <manager> ("managerdetail ");

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.