Silverlight HttpWebRequest SecurityException

Problem

I have a quiet web service running on a remote server. I already made a WP7 application that uses it, so I know that it works. I am porting an application to a Silverlight web application and have encountered a problem.

I included a simplified version of the code, as well as the error that was selected. The error is called by the EndGetResponse method.

Feel free to request more information. I have a search around solutions and have not found anything that works or really relates to my problem. It seems like such simple code, and it works great on the WP7 version. Any help would be appreciated.

Code

 void SendRequest() { string url = "http://some-example-restful-service:5600/locations/data/all"; HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/json; charset=utf-8"; request.BeginGetResponse(new AsyncCallback(RequestCallback), request); } public static void RequestCallback(IAsyncResult asyncResult) { HttpWebRequest request = (HttpWebRequest) asyncResult.AsyncState; HttpWebResponse response = (HttpWebResponse) request.EndGetResponse(asyncResult); //Parse JSON object response.Close(); //Dispatch result back to GUI thread } 

Error

SecurityException failed to execute user code

System.Security.SecurityException: Security Error.

 at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClass5.<EndGetResponse>b__4(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 ServieTest.MainPage.RequestCallback(IAsyncResult asyncResult) at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClassd.<InvokeGetResponseCallback>b__b(Object state2) at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() at System.Threading.ThreadPoolWorkQueue.Dispatch() at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback() 
+4
source share
2 answers

Turns out I need to add the clientaccesspolicy.xml file to the root of the service domain.

The following is the contents of the file:

 <?xml version="1.0" encoding="utf-8"?> <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> 
+7
source

In accordance with this answer, Silverlight (in a browser) rewards the cross-domain policy of the server from which you request data. Since this is your own web service, you can add the crossdomain.xml file to the root application, which allows external domains to perform cross-domain requests (for this answer ).

+1
source

Source: https://habr.com/ru/post/1383480/


All Articles