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 ");