13

我正在尝试让 Silverlight 使用快速示例应用程序,并在另一台计算机上调用休息服务。拥有其余服务的服务器有一个 clientaccesspolicy.xml,如下所示:

<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>

并且正在被拾取(至少根据我运行的网络跟踪),并且没有对 crossdomain.xml 的请求。C# 代码如下所示:

public Page()
{
    InitializeComponent();

    string restUrl = "http://example.com/rest_service.html?action=test_result";

    WebClient testService = new WebClient();
    testService.DownloadStringCompleted += new DownloadStringCompletedEventHandler(testService_DownloadStringCompleted);
    testService.DownloadStringAsync(new Uri(restUrl, UriKind.Absolute));

}

void testService_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    if (e.Error == null)
    {
        LoadTreeViewWithData(e.Result);
    }
}

但是,我总是收到以下安全错误:

{System.Security.SecurityException ---> System.Security.SecurityException:安全错误。
   在 System.Net.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
   在 System.Net.BrowserHttpWebRequest.c__DisplayClass5.b__4(对象 sendState)
   在 System.Net.AsyncHelper.c__DisplayClass2.b__0(对象 sendState)
   --- 内部异常堆栈跟踪结束 ---
   在 System.Net.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod,对象状态)
   在 System.Net.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   在 System.Net.WebClient.GetWebResponse(WebRequest 请求,IAsyncResult 结果)
   在 System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult 结果)}

我究竟做错了什么?为什么安全错误没有告诉我一些更有用的信息?

4

3 回答 3

8

如果您还没有这样做,我会首先尝试将 restUrl 更改为更简单的东西,例如同一服务器(或者如果需要在您自己的服务器上)上的静态 HTML 页面,以验证您的主要代码是否有效。

假设安全异常特定于该 REST URL(或站点),您可以查看Silverlight 2 中的 URL 访问限制文章。除了更广为人知的跨域规则之外,还有一些涉及文件类型和“Internet 区域”的非显而易见的安全规则。

我第二次抱怨 Silverlight 中的许多异常消息不是很有帮助。上面引用的 MSDN 文章包含一个有趣的注释:

当用户收到因违反这些访问策略之一而导致的错误时,该错误可能无法指示确切原因。

于 2008-10-31T21:29:29.547 回答
4

如果不将 http-methods="*" 添加到 clientaccesspolicy.xml 中的 allow-from 元素,我将无法执行跨域 REST HTTP 删除。当我添加 http-methods 属性时,一切正常,SecurityException 停止发生。

于 2011-08-18T20:35:27.557 回答
0

对于我的本地应用程序 ( http://localhost/ ) ,从“受信任的站点”加载 HTML 页面失败- 直到我将 localhost 添加到受信任的站点列表中。

Silverlight 防止“跨区域”调用(在我的情况下为本地网络与可信站点)和“跨方案”调用(例如 http 与 https)。

到目前为止,它只适用于“crossdomain.xml”文件。我首先尝试了“clientaccesspolicy.xml”,但没有成功。

于 2010-01-12T13:41:36.187 回答