Because silverlight is a client and does not support session, we sometimes need to use this session, for example, through asp tutorial. after logging on to the net login page and recording the session, I want to use this session in silverlight now. Some friends thought they could use the url parameter to pass the value. If this session represents a type of permission management, it is very dangerous to upload a url. Here is a solution for everyone. Although it is not absolutely secure, we can use encryption to ensure its maximum security.
Silverlight can call the js function. We will return the session value in the js function of the host page.
Host page js functions:
Function prompt (){
<% Session ["user"] = "swk"; %>; // defines the session
Return "<% = session [" user "]. tostring () %>"; // return session
}
Let's take a look at how silverlight uses this session:
Scriptobject urivirtual = htmlpage. window. getproperty ("prompt") as scriptobject;
Messagebox. show (urivirtual. invokeself (). tostring (); // output the returned value
To better reflect the code under the Application
Purpose
Use session in sl and keep it efficient!
Tutorial scheme:
Silverlight + asp.net tutorial + session + wcf
Code
Sessionproxy. svc class
// Note: If you change the class name "sessionproxy", you must also update the reference to "sessionproxy" in web. config.
[Aspnetcompatibilityrequirements (requirementsmode = aspnetcompatibilityrequirementsmode. allowed)]
Public class sessionproxy: isessionproxy
{
Public void dowork ()
{
}
// Read the session
Public object getsessionvariable (string key)
{
Return system. web. httpcontext. current. session [key];
}
// Modify the session
Public object setsessionvariablevalue (string key)
{
Return system. web. httpcontext. current. session [key];
}
// Add a session
Public void addsessionvariable (string key, object value)
{
System. web. httpcontext. current. session [key] = value;
}
// Delete a session
Public void removesessionvariable (string key, object value)
{
System. web. httpcontext. current. session. remove (key );
}
}
Interface Class:
Test code:
String username = "";
If (webdisaster. lib. tool. sessionmanager. session. containskey ("username "))
Username = webdisaster. lib. tool. sessionmanager. session ["username"]. tostring ();
P. getsessionvariableasync ("username ");
P. getsessionvariablecompleted + = (sender1, e1) =>
{
If (e1.result! = Null)
Username = e1.result. tostring ();
};
Webdisaster. login loginwin = new webdisaster. login ();
Loginwin. show ();