silverlight 跨域访问 wcf

先介绍一下我的测试项目,我用flash和silverlight一同来调用一个webservice,一个flash客户端,一个silverlight客户端,一个web项目来host flash和silverlight,再加上一个webservice端。
flash发布到web项目的swf文件夹下。 web项目中的clienttestpage.html中的sl和flash来调用webservice。

具体如何调用webservice我这里就略去了。
我的webservice里有一个方法
[WebMethod]public string SayHello(string name) { return "Hello " + name;}

webservice

Html Host页面

--------------------------------------------------------------------------------

这里先用silverlight来调用,在输入用户名都点击invoke按钮看看发声了什么。
在firefox中打开firebug的网络监视器

这里发现他先去webservice所在的域的根目录下请求一个 clientaccesspolicy.xml 的文件,在没有到后又去请求一个crossdomain.xml的文件,同样也是没有找到,返回404。

好,这里我在webservice域根下放入一个clientaccesspolicy.xml
clientaccesspolicy.xml
代码

<?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 include-subpaths="true" path="/"/> 
               </grant-to> 
          </policy> 
      </cross-domain-access>
  </access-policy>

因为webservice有requestheaders的请求,所以要加上红色部分。
再次调用看看是什么情况
请求到了clientaccesspolicy.xml 后就去真正请求webservice文件了。 并且得到了返回值。

接下来,去掉clientaccesspolicy.xml,加入crossdomain.xml
crossdomain.xml

<?xml version="1.0"?>
  <cross-domain-policy> 
     <allow-access-from domain="*"/>
     <allow-http-request-headers-from domain="*" headers="*"/>        
  </cross-domain-policy>

再次调用
在没有找到clientaccesspolicy.xml的情况下,去请求crossdomain.xml文件,得到响应后就正式请求webservice文件,并且也得到了返回值。

--------------------------------------------------------------------------------

接下来看flash 这里用as2.0做的。
他一开始直接就请求了crossdomain.xml,并没有去请求clientaccesspolicy.xml。
再输入用户名后,直接向webservice post数据,得到返回值。

好接下来看看as3的flash又会如何。

机制有些改变,一开始没去请求xml。

输入用户名,调用

再看看没有找到xml的情况


--------------------------------------------------------------------------------

好了在看看网上我搜索到的一些说法。

“出于安全考虑,FlashPlayer默认的安全设置是不允许进行跨域操作的。即便是同一个网站,如果用不同的方式访问,也会被FlashPlayer认为是跨域操作。
为解决Flash/Flex系统中的跨域问题,提出了crossdomain.xml跨域策略文件。有了它,就可以解决跨域问题。”

“SilverLight要实现跨域访问,必须在服务端被访问域的直接域名下,配置 clientaccesspolicy.xml( 或 crossdomain.xml)文件,即可以访问 http://{domainName}/clientaccesspolicy.xml。 ”
--------------------------------------------------------------------------------


提出问题
关于crossdomain.xml 和 clientaccesspolicy.xml 的区别。
1、这两个文件真的是可以任选其一吗?
2、这两个文件分别需要被放在服务端还是客户端?
3、不管后台服务部署在IIS还是其它环境中,这两个文件都可以使用吗?


解答
1.silverlight在使用中可以任选其一,建议直接使用clientaccesspolicy.xml。flash只能使用crossdomain.xml。
2.都是必须在服务端被访问域的直接域名下 。
3.都可以使用

Silverlight supports two different mechanisms for services to opt-in to cross-domain access:
? Place a clientaccesspolicy.xml file at the root of the domain where the service is hosted to configure the service to allow cross-domain-access.
? Place a valid crossdomain.xml file at the root of the domain where the service is hosted. Silverlight supports a subset of the crossdomain.xml schema. This file format is supported as well by ADOBE FLASH (originally by them).
clientaccesspolicy.xml is for silverlight
view plaincopy to clipboardprint?

<?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 include-subpaths="true" path="/"/> 
                </grant-to> 
            </policy> 
        </cross-domain-access> 
    </access-policy> 

crossdomain.xml is for flash and also silverlight
view plaincopy to clipboardprint?

<?xml version="1.0"?> 
    <!DOCTYPE cross-domain-policy 
SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"> 
        <cross-domain-policy> 
            <allow-access-from domain="*" headers="*" /> 
        </cross-domain-policy> 
原文地址:https://www.cnblogs.com/CharlesGrant/p/3653492.html