使用Silverlight消费JSON会引发SecurityException [英] Consuming JSON using Silverlight throws SecurityException

查看:97
本文介绍了使用Silverlight消费JSON会引发SecurityException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在单击按钮时从Silverlight应用程序使用JSON服务.但是,在读取JSON服务网址时,它会引发以下异常.

I am trying to consume the JSON service from a Silverlight application on button click. But while reading the JSON service url, it throws the below exception.

System.Security.SecurityException ---> System.Security.SecurityException: Security error.
   at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
   at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClassa.<EndGetResponse>b__9(Object sendState)
   at System.Net.Browser.AsyncHelper.<>c__DisplayClass4.<BeginOnUI>b__0(Object sendState)
   --- End of inner exception stack trace ---
   at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
   at System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result)
   at System.Net.WebClient.OpenReadAsyncCallback(IAsyncResult result)

下面是我的代码:

private void Button_Click(object sender, RoutedEventArgs e)
        {
            WebClient client = new WebClient();
            client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
            string url = "http://www.javascriptkit.com/dhtmltutors/javascriptkit.json";
            client.OpenReadAsync(new Uri(url));
        }

        private void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
              StreamReader reader = new StreamReader(e.Result);
        }

请帮助我修复该问题

我在Web文件夹中添加了clientaccesspolicy.xml.但是,选中Fiddler时会说:在此服务器上找不到请求的URL/clientaccesspolicy.xml".但是我可以从我的URL访问此xml.

I added clientaccesspolicy.xml in web folder. But when Fiddler is checked it says, "The requested URL /clientaccesspolicy.xml was not found on this server" . But I could access this xml from my url.

    <?xml version="1.0" encoding="utf-8" ?> 
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="*"/>
<domain uri="http://*"/>
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>

推荐答案

在允许连接到网络资源之前,Silverlight运行时将尝试从承载网络资源的域中下载安全策略文件.有两种不同的下载安全策略的方法,具体取决于连接请求是来自WebClient还是HTTP类,还是连接请求是来自套接字.

Before allowing a connection to a network resource, the Silverlight runtime will try to download a security policy file from the domain that hosts the network resource. There are two different methods used to download the security policy that depend on whether the connection request was from a WebClient or HTTP class or whether the connection request was from sockets.

如果连接请求是从WebClient或HTTP类到跨域站点的,则Silverlight运行时将尝试使用HTTP协议下载安全策略文件. Silverlight运行时首先尝试使用HTTP协议在请求的目标域的根目录下载名称为"clientaccesspolicy.xml"的Silverlight策略文件.

If the connection request was from a WebClient or an HTTP class to a cross-domain site, the Silverlight runtime tries to download the security policy file using the HTTP protocol. The Silverlight runtime first tries to download a Silverlight policy file with a name of "clientaccesspolicy.xml" at the root of the requested target domain using the HTTP protocol.

如果未找到"clientaccesspolicy.xml"(Web请求返回404状态代码),返回的MIME类型意外,无效的XML或具有无效的根节点,则Si​​lverlight运行时将使用HTTP协议在请求的目标域的根目录下发出对Flash策略文件的请求,其名称为"crossdomain.xml".

If the "clientaccesspolicy.xml" is either not found (the web request returns a 404 status code), returned with an unexpected mime-type, is not valid XML, or has an invalid root node, then the Silverlight runtime will issue a request for the Flash policy file with a name of "crossdomain.xml" at the root of the requested target domain, using the HTTP protocol.

不允许策略文件的HTTP重定向.重定向策略文件将导致拒绝访问的SecurityException.

HTTP redirects for the policy file are not allowed. A redirect for a policy file will result in a SecurityException of access denied.

这篇关于使用Silverlight消费JSON会引发SecurityException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆