How to solve the security problem of Silverlight cross-origin consumption WCF Service

Source: Internet
Author: User

This is an old problem, and I also encountered it, that is, providing services through WCF,SilverlightConsume the service in the previous section. If the address provided by WCF is http:/localhost: 8839/, and the port running by Silverlight is inconsistent, for example, http: // localhost: 7777/, it is not in a domain, cross-origin access occurs. By defaultWebCross-origin is blocked by security.

I checked it online. The main solution is to add a clientacesspolicy. xml file to the root path of WCF. TheXMLThe content is

<? XML version = "1.0" encoding = "UTF-8"?>
<Access-Policy>
<Cross-domain-access>
<Policy>
<Allow-from http-request-headers = "*">
<Domain uri = "*"/>
</Allow-from>
<Grant-to>
<Resource Path = "/" include-subpaths = "true"/>
</Grant-to>
</Policy>
</Cross-Domain-access>
</Access-Policy>

However, the addition does not work. I checked it online to provide it as a Service. Later, based on the wxwinter example, this example can beGoogleAdd a service to solve the problem.

The Service Interface of this service is as follows:

[Servicecontract]
Public interface icrosdomainservice
{
[Operationcontract]
[Webget (uritemplate = "clientaccesspolicy. xml")]
Message providepolicyfile ();

}

Specific services:

Public class crossdomainservice: icrosdomainservice
{

Public System. servicemodel. channels. Message providepolicyfile ()
{

Filestream = file. Open (@ "cross-origin access \ clientacesspolicy. xml", filemode. Open, fileaccess. Read, fileshare. readwrite); // access the XML address here
Xmlreader reader = xmlreader. Create (filestream );

System. servicemodel. channels. Message result = message. createmessage (messageversion. None, "", Reader );
System. Console. writeline ("it start ...");
Return result;

}
}

Default questions about app Configuration

<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>

<! -- When deploying a service library project, you must add the configuration file content
In the app. config file of the host. System. configuration does not support database configuration files. -->
<System. servicemodel>
<Servicehostingenvironment aspnetcompatibilityenabled = "true"/>
<Behaviors>
<Endpointbehaviors>
<Behavior. Name = "crossdomainservice. crossdomainservicebehavior">
<Webhttp/>


</Behavior>
</Endpointbehaviors>
<Servicebehaviors>

</Servicebehaviors>
</Behaviors>

<Services>
<Service name = "crossdomainservice. crossdomainservice">
<Endpoint address = "" behaviorconfiguration = "crossdomainservice. crossdomainservicebehavior"
Binding = "webhttpbinding" Contract = "crossdomainservice. icrosdomainservice"/>
<Host>
<Baseaddresses>

<Add baseaddress = "http: // localhost: 7656/"/>

<! --- Baseaddress: the address that you want to provide cross-origin access -->
</Baseaddresses>
</Host>
</Service>

</Services>
</System. servicemodel>
</Configuration>

After you add this service to the WCF Service, the domain http: // localhost: 7656/can provide cross-origin access.

Check whether it is successful. You can view http: // localhost: 7656/clientacesspolicy. XML to see whether the XML document is displayed.

TheSource codeIn the attachment, change crossdomainservice.gifto crossdomainservice.rar.

Http://space.itpub.net/22259926/viewspace-627039#xspace-itemreply

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.