2013-09-16 20 views
1

我创建了Web服务,并且正在从Silverlight应用程序调用。System.Security.SecurityException在Silverlight中调用Web服务

我得到内部异常,如:

{System.Security.SecurityException ---> System.Security.SecurityException:安全错误。 at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) at System.Net.Browser.BrowserHttpWebRequest。 <> c_ DisplayClassa.b _9(Object sendState) at System.Net.Browser.AsyncHelper。 <> C_ DisplayClass4.b _0(对象sendState)内部异常堆栈跟踪的 --- ---结束在 System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod,对象状态) 在System.Net。 Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult的asyncResult) 在System.Net.WebClient.GetWebResponse(WebRequest的请求,IAsyncResult的结果) 在System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult的结果)}

堆栈跟踪:

“在System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod,Objec在System.Net.WebClient上System.Net.WebClient.GetWebResponse(WebRequest请求,IAsyncResult结果)\ r \ n下System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)\ r \ n \ r \ n \ r \ .DownloadBitsResponseCallback(IAsyncResult的结果)”

当我谷歌这样的错误:

我才知道,这是跨域网址,所以我必须在添加C clientaccesspolicy.xml和crossdomain.xml文件的问题: \ inetpub \ wwwroot

还是我得到同样的错误:

让我知道如何解决这个错误。

下面的代码,我已经使用:

  System.Uri uri = new System.Uri("https://[localhost]/CustomerPortalService12/AddAccount/" + "AccountName"); 


      var result = ""; 
      try 
      { 
       var webClient = new WebClient();     

        webClient.DownloadStringCompleted +=webClient_DownloadStringCompleted;      
        webClient.DownloadStringAsync(uri);      
      } 
      catch (Exception ex) 
      { 
       var wtf = ex.Message; 
      } 
     } 
    } 

     void webClient_DownloadStringCompleted(object sender, System.Net.DownloadStringCompletedEventArgs e) 
     { 

     } 

回答

0

确保clientaccesspolicy.xml包含您所使用的浏览器的域。如果你在本地调试,这可能是localhost。 clientaccesspolicy.xml必须位于托管服务的域的根目录下。如果您在本地以及Silverlight项目中托管服务,请确保您的bowser可以通过http://localhost/clientaccesspolicy.xmlhttps://localhost/clientaccesspolicy.xml访问该文件,具体取决于您如何致电该服务。否则,将localhost替换为服务所在的域。

你clientaccesspolicy.xml应该是这个样子:

<?xml version="1.0" encoding="UTF-8"?> 
-<access-policy> 
-<cross-domain-access> 
<!--May have multiple elements--> 
-<policy> 
-<allow-from http-request-headers="*"> 
<domain uri="https://localhost"/> 
</allow-from> 
-<grant-to> 
<resource include-subpaths="true" path="/"/> 
</grant-to> 
</policy> 
</cross-domain-access> 
</access-policy>