如何跨域访问Silverlight XAP文件?

时间:2009-06-26 21:57:39

标签: silverlight cross-domain

我正在尝试将位于一个子域中的Silverlight应用程序添加到另一个子域中的网页。由于某些原因,这只是简单的不起作用...我的Silverlight应用程序在http://subA.domain.com/somepage.html的页面中加载:

<div id="silverlightControlHost">
    <object data="data:application/x-silverlight-2," type="application/x-silverlight-2"
        width="800px" height="600px">
        <param name="source" value="http://subB.domain.com/SilverlightApp.xap" />
        <param name="onerror" value="onSilverlightError" />
        <param name="background" value="white" />
        <param name="minRuntimeVersion" value="2.0.31005.0" />
        <param name="autoUpgrade" value="true" />
        <param name="enableHtmlAccess" value="true" />
        <a href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;">
            <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight"
                style="border-style: none" />
        </a>
    </object>
    <iframe style='visibility: hidden; height: 0; width: 0; border: 0px'></iframe>
</div>

如果我将SilverlightApp.xap移动到subA.domain.com,它会完美加载。跨域访问XAP文件需要哪些步骤?我一直在试图解决这个问题,似乎无处可去。

谢谢!

3 个答案:

答案 0 :(得分:5)

为了帮助其他人遇到同样的问题,并且不想使用IFrame,请参阅此link,因为它已解决了我的问题。即使作者指的是Silverlight 2,它也解决了Silverlight 3中的问题。如果链接断开,我需要做两件事:

- 在Silverlight应用程序中,编辑AppManifest.xml以添加以下内容:

<Deployment xmlns="http://schemas.microsoft.com/client/2007/deployment"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
ExternalCallersFromCrossDomain="ScriptableOnly">

- 如果您在Silverlight应用中使用HtmlPage(例如在阅读传递给托管页面的QueryString时),您还必须添加:

<param name="enableHtmlAccess" value="true" />

到托管页面中的silverlight对象。

请注意以上内容存在安全隐患,我不禁想到这就是为什么微软不会尽力传播这些信息的原因。但是在我的情况下,我没有可编写脚本的silverlight元素,而且自从我编写了silverlight应用程序以来,我对主机页面没有问题,允许silverlight应用程序访问它。

在研究这个问题时,我注意到这个问题和相应的解决方案与一个单独的问题相混淆,即Silverlight xap跨域边界访问wcf服务的问题。该问题确实需要位于托管wcf服务的网站根目录下的clientaccesspolicy.xml文件。

因此,第一个站点可以访问第二个站点上的xap文件,该站点访问第三个站点上的数据服务,以获得最大的灵活性和重用。

答案 1 :(得分:2)

当Silverlight跨域请求.XAP文件时,内容类型必须为: application / x-silverlight-app 。此外,您需要在其他域上使用跨域策略文件。 GL

答案 2 :(得分:0)

您可以在包含silverlight对象的.xap旁边创建一个简单的html文件,并从iframe访问它。这就是http://silverlight.live.com/解决此问题的方法。

在subA.domain.com上的主页面上添加一个在其他域名上显示html页面的iframe:

<iframe src="http://subB.domain.com/SilverlightApp.html" 
        scrolling="no" 
        frameborder="0" 
        style="width:800px;height:600px">
</iframe>

和subBdomain.com上的SilverlightApp.html可能类似于:

<html>
  <body>
    <div id="silverlightControlHost">    
      <object data="data:application/x-silverlight-2," 
              type="application/x-silverlight-2"        
              width="800px" height="600px">        
        <param name="source" value="http://subB.domain.com/SilverlightApp.xap" />
        <param name="onerror" value="onSilverlightError" />
        <param name="background" value="white" />
        <param name="minRuntimeVersion" value="2.0.31005.0" />
        <param name="autoUpgrade" value="true" />
        <param name="enableHtmlAccess" value="true" />
        <a href="http://go.microsoft.com/fwlink/?LinkID=124807" 
           style="text-decoration: none;">            
          <img src="http://go.microsoft.com/fwlink/?LinkId=108181" 
               alt="Get Microsoft Silverlight"
               style="border-style: none" />
        </a>
      </object>
      <iframe style='visibility: hidden; height: 0; width: 0; border: 0px'>
      </iframe>
    </div>
  </body>
</html>
相关问题