Usage Analysis of Asp.net sessions in SilverLight

Source: Internet
Author: User

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

Related Article

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.