2013-09-16 52 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>