Thursday, May 27, 2010

Silverlight security error connecting SharePoint

Scenario:
Wrote a small Silverlight application which was trying to display the title of the site using Client Object Model and to my surprise I got the following error :

{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.OpenReadAsyncCallback(IAsyncResult result)}


Solution:
This can be because of few reasons

1. Make sure the web URL used to create the Client context object exist and brows-able.

2. This might be because of missing client policy access file missing for the SharePoint Web Application not allowing you to connect. Create a new XML file named ClientAccessPolicy.xml with content below and save it to 'C:\inetpub\wwwroot\VirtualDirectories\80'

Note : Update policy file and port number as needed.

ClientAccessPolicy.xml:

<?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>
Once you have the file copied to right location, do an IISReset and make sure you can access the file using following url :

http://servername:portno/clientaccesspolicy.xml
Note
This setting is only for Development Environment. Read the following article how to tweak the policy file for production environment.

Article:MSDN

3 comments:

Anonymous,  May 10, 2012 at 3:29 AM  

Great solution.

We were facing same error and taking your steps mentioned resolved the issue.

Anonymous,  May 10, 2012 at 3:29 AM  

Great solution.

It solved our problem.

Anonymous,  June 6, 2014 at 10:25 AM  

Thanks man...good solution. Saves me a lot time for debugging my project!