Beetle simple implementation of flash and Silverlight Policy Server

Source: Internet
Author: User

Recently, I want to write some socket communication cases for flash and Silverlight. Because the sockets of the two require a prolicy server for security consideration, I found it on the Internet onlyCodeNoProgramDirectly, beetle is used to implement the policy server of flash and Silverlight. Flash or Silverlight is enabled Based on the configuration when used. if you want to enable both of them at the same time, the following describes the implementation process.

First, we must know what the flash and Silverlight trusted files are.

Flash

<Cross-Domain-Policy><Site-controlPermitted-Cross-Domain-policies= "Master-only"/><Allow-access-fromDomain= "*"To-Ports= "*"Secure= "False"/></Cross-Domain-Policy>

Silverlight

<?  XML version = "1.0" encoding = "UTF-8"  ?>  <  Access-Policy  >    <  Cross-Domain-Access  >      <  Policy  >        <  Allow-from  >          < Domain  Uri  = "*"  />        </  Allow-from  >        <  Grant-  >          <  Socket-Resource  Port  = "4502-4534"  Protocol  = "TCP"  />       </  Grant-  >      </  Policy  >    </  Cross-Domain-Access  >  </  Access-Policy  > 

The two work in the form of Flash request port 843, while Silverlight is port 943. The server receives the request information and returns the actual trust XML Information Based on no conditions. The following is a simple service implementation based on beetle.

Class program: serverbase {static void main (string [] ARGs) {console. writeline ("Silverlight and flash prolicy server copyright @ kender 2012"); console. writeline ("Website: www.ikender.com"); console. writeline ("Email: henryfan@msn.com"); console. writeline ("QQ: 28304340"); beetle. tcputils. setup ("beetle"); If (prolicysection. instance. flashenabled) {program flashprolicy = new program (); flashprolicy. type = Prolicytype. flash; flashprolicy. open (prolicysection. instance. flashport); console. writeline ("START flash prolicy server @" + prolicysection. instance. flashport);} If (prolicysection. instance. slenabled) {program flashprolicy = new program (); flashprolicy. type = prolicytype. silverlight; flashprolicy. open (prolicysection. instance. slport); console. writeline ("START Silverlight prolicy server @" + pro Licysection. instance. slport);} console. read ();} public prolicytype type {Get; set;} public Enum prolicytype {flash, Silverlight} protected override void onconnected (Object sender, channeleventargs e) {base. onconnected (sender, e); E. channel. enabledsendcompeletedevent = true; E. channel. sendmessagecompleted = (O, m) => {M. channel. dispose () ;}; console. writeline ("{0} connected! ", E. channel. endpoint);} protected override void ondisposed (Object sender, channeldisposedeventargs e) {base. ondisposed (sender, e); console. writeline ("{0} disposed! ", E. channel. endpoint);} protected override void onerror (Object sender, channelerroreventargs e) {base. onerror (sender, e); console. writeline ("{0} error {1 }! ", E. channel. endpoint, E. exception. message);} protected override void onreceive (Object sender, channelreceiveeventargs e) {base. onreceive (sender, e); console. writeline (E. channel. coding. getstring (E. data. array, E. data. offset, E. data. count); stringmessage message = new stringmessage (); If (type = prolicytype. flash) {message. value = utils. getflashpolicy ();} else {message. value = utils. getslpolicy ();} e. channel. send (Message );}}

the program object inherits serverbase and then overrides several related methods to output a trusted file in the onreceive event based on different types of the current server. the utils class mainly encapsulates the reading of trusted file information. The Code is as follows:

Class utils {static string mslpolicy = NULL; static string mflashpolicy = NULL; public static string getslpolicy () {If (mslpolicy = NULL) {string filename = appdomain. currentdomain. basedirectory + "slpolicy. XML "; using (system. io. streamreader reader = new system. io. streamreader (filename, encoding. utf8) {mslpolicy = reader. readtoend () ;}} return mslpolicy;} public static string getflashpolicy () {If (mflashpolicy = NULL) {string filename = appdomain. currentdomain. basedirectory + "flashpolicy. XML "; using (system. io. streamreader reader = new system. io. streamreader (filename, encoding. utf8) {mflashpolicy = reader. readtoend () + "\ 0" ;}} return mflashpolicy ;}}

The policy server that supports both Flash and Silverlight is complete, isn't it easy.

Download this tool

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.