【问题标题】:Silverlight Rest Service, Security ExceptionSilverlight Rest 服务,安全异常
【发布时间】:2008-10-31 20:46:53
【问题描述】:

我正在尝试让 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 结果)}

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

【问题讨论】:

  • 您需要更改哪些内容才能使其正常工作?
  • 当您的 Silverlight 项目不是 Web 项目时,您无法调用 REST 服务。

标签: c# silverlight rest


【解决方案1】:

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

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

我赞同关于 Silverlight 中的许多异常消息没有多大帮助的抱怨。上面引用的 MSDN 文章包含一个有趣的注释:

当用户收到因违反其中一项访问政策而导致的错误时,该错误可能无法说明确切原因。

【讨论】:

  • 啊,鲜为人知的跨区限制……我花了好几天才弄清楚是什么导致了我的问题。感谢您的链接。 +1
【解决方案2】:

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

【讨论】:

  • 谢谢你 - 这也是我的问题。
【解决方案3】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-08
    • 1970-01-01
    • 1970-01-01
    • 2013-01-31
    • 2010-12-11
    • 1970-01-01
    • 2011-03-20
    • 1970-01-01
    相关资源
    最近更新 更多