代码之家  ›  专栏  ›  技术社区  ›  Kris Erickson

Silverlight Rest服务,安全异常

  •  13
  • Kris Erickson  · 技术社区  · 17 年前

    我正在尝试让Silverlight使用一个快速示例应用程序,并调用另一台计算机上的rest服务。具有rest服务的服务器有一个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: Security error.
       at System.Net.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
       at System.Net.BrowserHttpWebRequest.c__DisplayClass5.b__4(Object sendState)
       at System.Net.AsyncHelper.c__DisplayClass2.b__0(Object sendState)
       --- End of inner exception stack trace ---
       at System.Net.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
       at System.Net.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
       at System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result)
       at System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result)}
    

    3 回复  |  直到 17 年前
        1
  •  8
  •   C. Dragon 76    17 年前

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

    URL Access Restrictions in Silverlight 2 文章除了更著名的跨域规则外,还有一些涉及文件类型和“internet区域”的不明显的安全规则。

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

    当用户收到由于违反其中一个访问策略而导致的错误时,该错误可能不会指明确切的原因。

        2
  •  4
  •   Ruth    14 年前

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

        3
  •  0
  •   I liked the old Stack Overflow    16 年前

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