Advertisement

Silverlight cross domain policy file helpers

If you are starting to get into integrating web services with Silverlight, you'll notice that you have to have a cross domain policy file in place on the target server, that is to say, the server hosting the service you want to implement.  There are some public web services (Flickr, YouTube, Digg, etc.) that already have these files in place for Flash, but implement in a slightly different way.

When calling a cross-domain service, Silverlight will check for the existence of clientaccesspolicy.xml first.  This is the format defined by Silverlight and provides a pretty flexible way to define who can access what services.  If not found, it will then default to look for crossdomain.xml, which is the file format implemented for Adobe Flash.  It is important to note that this file will also still work for most public web services.

But now perhaps you are the author of the service that your application is going to consume and/or the public will consume.  There are a few things you want to consider.  First, it would be a best practice to put your service layer on a separate domain other than your site (i.e., api.mysite.com).  In fact, this is how most are doing it these days.  These helps separate more distinctly the services from the web site and also separates the cross-domain security concerns away from the content site versus API access.  Once you have done that you'll want to implement your specific clientaccesspolicy.xml file.

When Silverlight 2 was released to beta, I created some quick helper files to assist me with creating this simple policy file (it is simple, but can get complex depending on how granular you want to define your access).  I figured it might be helpful to some who are implementing services as well.  Sure, they aren't going to save the world, but might save you some quick typing.

Visual Studio Code Snippet

The slcap.vsi file is a Visual Studio Community Installer package which contains "slcap.snippet," which is a Visual Studio code snippet format.  This is an XML snippet, so would be used only in the context of an XML file.  Just double-click on the .vsi file to install and it will walk you through the steps.  I recommend just keeping the defaults.  After it is complete, you now have an Intellisense snippet.  To use it and create a new clientaccesspolicy.xml add a new XML file to your web service site/project named clientaccesspolicy.xml.  It will open a blank XML file by default.  Select all text (CTRL+A).  Then hit the keyboard shortcut for launching XML snippets, CTRL+K,X.

NOTE: For some reason XML snippets don't operate like C#/VB ones do where you type the shortcut, then TAB, TAB.  If anyone knows why, let me know :-)

This will bring up the navigator, then simply navigate to the My XML Snippets, then locate the one you just installed:

Once you select it, there are three literal areas to override the defaults if you wanted. 

UPDATE: As Sean probably ran into below (in comments), the above screenshot does not show the required http-request-headers attribute required on the allow-from node of the policy file.  This is, however, updated in the Intellisense files and the code snippet template.  Thanks Sean for pointing out the screenshot is wrong here.

If you are implementing a completely public web service (open to anyone for cross-domain access), then the defaults will suffice.  When done changing the values, hit enter and you are done.  For those who are not keyboard shortcut masters and would be using a mouse to do all this, it might not be terribly faster to be honest (if the TAB,TAB implementation was there for XML snippets, it would eliminate the arrow up/down to navigate to the snippet).

Get the slcap.snippet here.

Visual Studio Intellisense Files

This next step is a super hack that I originally did and decided it might not be a good idea, but I'll include it here anyway :-).  This involves adding Intellisense files to your VS2008 installation and if you aren't comfortable with that, then move along.

First, you'll want to get the XSD I created, which is very simple and I'm sure doesn't fully conform to the final spec, but lacking that spec, it maps to the format well enough.  Copy the clientaccess.xsd file to the C:\Program Files\Microsoft Visual Studio 9.0\Xml\Schemas location (or wherever VS2008 is installed for you).  Once you've done that you have to add an entry into the catalog.xml file to add the mapping.  Again, not this is my little hack so I created some namespace because there wasn't one defined yet.

Once you have those two files you have Intellisense for your clientaccesspolicy.xml file if you want it.  Following similar steps as above, create the new file.  This time, however, type the root node of <access-policy> but adding the 'xmlns' attribute pointing to the new namespace you just added to the catalog file (note: Intellisense should give you a list to choose from:

Once you have that, then you'll get the rest of the Intellisense for the basic format of the client access policy format.  If you have multiple allow-from/grant-to needs, this Intellisense will support it.

The only lame thing is you have my namespace in there :-).  That is what drives the Intellisense.  Right now you'll want to remove that before deploying the actual file.  Yeah, I know.  But I said this was an early hack of mine didn't I?

Get the Intellisense files here.

What do to with the completed policy file?

Either way, when you are done with the file, it needs to go in the ROOT of the domain.  This is important as it is not the application root, but the root web.  Even if your app is at foo.com/myapp, the policy file needs to be at foo.com/clientaccesspolicy.xml.

Anyhow, maybe these files will help you.  Ideally you won't be using/messing with an access policy file much, but this might save you some clicks and having the docs open next to you :-).

  1. 4/7/2008 4:19 AM | # re: Silverlight cross domain policy file helpers
    Hey Tim :)

    maybe you wanna mention that cross doamin policies are currently not supported for SSL (i.e. HTTPS).

    Cheers,
    Christian
  2. 4/7/2008 5:44 AM | # re: Silverlight cross domain policy file helpers
    Hi Tim

    Any clue if this supported by silverlight streaming services? I tried several time to get access to the file but doesn't seems to work.

    Regards
    Laurent Kempé
  3. 4/7/2008 8:22 AM | # re: Silverlight cross domain policy file helpers
    very nice! no more writing web services that simply relay requests. What will I do with all my free time?
  4. 4/7/2008 2:25 PM | # re: Silverlight cross domain policy file helpers
    Hi Tim,

    Would this apply to Silverlight 1.0 or just 2?
  5. 4/7/2008 3:00 PM | # re: Silverlight cross domain policy file helpers
    Byron: thanks for the comment for clarification! Silverlight 1.0 does not support cross-domain calls so this applies for Silverlight 2
  6. 4/9/2008 7:34 AM | # re: Silverlight cross domain policy file helpers
    Hey Laurent and Tim - I'm interested in the same -- the extent to which cross-domain calling is supported by apps running on the Streaming service.

    Thanks for a great article Tim! Installed these bits into my VS.
  7. 4/9/2008 12:42 PM | # re: Silverlight cross domain policy file helpers
    Rob: thanks for the note. See http://timheuer.com/blog/archive/2008/04/09/cross-domain-in-silverlight-streaming.aspx for an example of a streaming app calling a cross-domain service!
  8. 5/2/2008 4:08 AM | # re: Silverlight cross domain policy file helpers
    Tim,
    I have an issue with using a semicolon in the url. I want to call an api, in this format http://somesite/page;xml and this is working when I call it through various different ways. But is not working through the silverlight 2.0 beta 1 release. I think this is being trapped by the policy file in some way, and refused access. Have you any idea if this is the case?
    peter
  9. 5/2/2008 2:56 PM | # re: Silverlight cross domain policy file helpers
    @peter: this is confirmed an issue for beta 1. this was due to some path comparison logic to make the path parsing as secure as possible. the list of allowed characters is expanding and semicolon is already checked in.
  10. 5/26/2008 5:48 AM | # re: Silverlight cross domain policy file helpers
    I installed the file but I'm still getting a security exception -CrossDomainError-. Could someone please point me in the right direction.
  11. 6/2/2008 9:03 PM | # re: Silverlight cross domain policy file helpers
    Thanks for timheuer great work!
    but there is still some problem here which puzzle me for one day . Maybe Peter and Steghen got the same mistake like me.

    The difference is the clientaccesspolicy.xml .if you create the XML file by the given snippet . There will be a "http-request-headers" attribute with the <allow-from> node . By the default from the MSDN , there is no such attribute with the <allow-from> node.

    remove the attribute from the node , it work ok!

  12. 6/2/2008 9:37 PM | # re: Silverlight cross domain policy file helpers
    @Henley: yes you are correct. I recently just updated my code snippet and the link. This new attribute will actually be required for Silverlight 2 Beta 2 and future. I put it in there now to prepare. If you are still using Beta 1, remove it and you will be fine.
  13. 7/3/2008 10:29 PM | # re: Silverlight cross domain policy file helpers
    I'm using a clientaccesspolicy.xml file as outlined here http://silverlight.net/forums/p/19812/68059.aspx#68059 but it doesn't work. I'm running the VS.NET 2008 built in web server. I'm not sure where its virtual path is referencing. I've placed the file all over but still get the security error. Running SL 2 beta 2.
  14. 7/30/2008 11:47 PM | # re: Silverlight cross domain policy file helpers
    Thanx You.. Perfect Docs
  15. 8/28/2008 4:31 AM | # re: Silverlight cross domain policy file helpers
    Cheers for the post. I was wondering about this. I had to design a small widget with ActionScript and they too use a cross domain policy like this:


    www.macromedia.com/.../cross-domain-policy.dtd">




    I was then wondering if SilverLight has something similar and here is the answer lol. It is chicken and the egg, I think that ActionScript 3.0 had it first but I do prefer the cleaner schema of the SilverLight policy.

    Cheers,

    Andrew
  16. 11/3/2008 7:32 PM | # re: Silverlight cross domain policy file helpers
    Hi, Tim
    Thank you for your excellent topic!
    I'm a novice of Silverlight.
    I wrote a ASP.NET web service and exposed a webmethod named 'Add'. I put it in my IIS server and can access it through http://zen:22222/Calculator.asmx
    Then I created a stand-alone Silverlight 2 project and referenced this web service.
    And, I created a XML file named clientaccesspolicy.xml and put is as same folder as the Calculator.asmx file. The content is same as the picture in you topic.
    But when I debug the Silverlight project, it still throw out a exception tell me can not access the web service cross-domain.
    What should I do?

    Best Regards,
    Tim, Liu
  17. 11/13/2008 11:54 AM | # re: Silverlight cross domain policy file helpers
    So, just to verify, Silverlight does NOT support cross-domain access with SSL and basic authentication, correct? Is this something that Flash DOES support? What recourse does a .Net and Silverlight advocate like myself have if this is a requirement of the project?
  18. 11/13/2008 4:40 PM | # re: Silverlight cross domain policy file helpers
    Mike: Silverlight can support x-domain SSL services (the service has to opt in). As to basic authentication -- you can't specifically set authenticaiton headers in network calls in Silverlight. I'm not sure if Flash supports this or not. We use the browser's network stack and thus we use the authentication already provided via the hosting app of the Silverlight content. If that is the same for your service it should work.
  19. 11/26/2008 6:45 PM | # re: Silverlight cross domain policy file helpers
    Hi, All

    1. I host a web service.

    2. Host a silverlight web application on a IIS

    The web service and sl app are on different machines.

    Case1: Silverlight App's ServiceReferences.ClientConfig's service address is : http://IPAddress:Port/...

    The I visit the silver light app both by http://IP/test.html or http://DNSName/test.html. Click the button that invokes the web service, it works correctly.

    Case 2: Silverlight App's ServiceReferences.ClientConfig's service address is : http://DNSName:Port/...
    The I visit the silver light app both by http://DNSName/test.html. Click the button that invokes the web service, it works correctly. Visit by http://IPAddress/test.html, then click the button, it throw exception of:

    CrossDomain issue. Actually, I have already set the crossdomain.xml and clientaccesspolicy.xml which based on BreakingChangesBetweenBeta2andRelease.doc. My silverlight version is R2 Release.

    So is that the latest of Silverlight's bug?

    Thanks
  20. 12/2/2008 7:50 PM | # re: Silverlight cross domain policy file helpers
    nairenxunwei: have you looked at the traffic to verify that a policy file is being requested?
  21. 1/19/2009 5:19 PM | # re: Silverlight cross domain policy file helpers
    I just wasted a few hours using the poorly constructed clientaccesspolicy.xml sample in this blog. What finally worked for me was the following:

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

    Sean
  22. 1/19/2009 5:25 PM | # re: Silverlight cross domain policy file helpers
    Sean depending on your need, this particular blog post doesn't have a poorly constructed cap.xml file at all...if so, point it out to me. I've indicated that this is for public consumption. It looks like your need was for a particular action (SOAP request) and to allow non-secure callers as well, something I also posted about here timheuer.com/.../...h-silverlight-2-ssl-https.aspx which has the exact policy you just posted in the comments.
  23. 1/19/2009 7:29 PM | # re: Silverlight cross domain policy file helpers
    Tim, please allow me reword my poorly worded post. The clientaccesspolicy.xml sample listed on this blog does not accommodate Silverlight connectivity to an IIS-hosted WCF service application, whereas the sample CAP I provided includes the missing pieces to facilitate this. In all fairness, your article was written well prior to 2.0 so it wasn't possible to foresee this shortcoming. My suffering was not your fault and I do apologize for my choice of words.

    Sean
  24. 1/19/2009 9:04 PM | # re: Silverlight cross domain policy file helpers
    Sean, ah yes the *screenshots* here are incorrect in one regard that http-request-headers must be set (even if "*"). The intellisense files and template, however, were updated.
  25. 2/12/2009 1:28 PM | # re: Silverlight cross domain policy file helpers
    Tim, I need to use x-domain ssl services. Do you have an example of how the service can opt in?
  26. 2/12/2009 3:56 PM | # re: Silverlight cross domain policy file helpers
  27. 3/28/2009 4:53 AM | # re: Silverlight cross domain policy file helpers
    After struggelig for dayw trying to get a cross domain web service to work, i finally found a solution.

    My setup was a silverlight service hosted under a https address. This silverlight app needed to do a cross domain wcf service call.

    I found that the silverlight app would call my clientaccesspolicy.xml file wich was setup just as demonstrated here, but with no luck.

    When i finally tried to change from:
    <allow-from http-request-headers="SOAPAction">
    <domain uri="http://*"/>
    <domain uri="https://*" />
    </allow-from>
    to
    <allow-from http-request-headers="SOAPAction">
    <domain uri="http://*"/>
    <domain uri="https://prosjektweb.domain.no" />
    </allow-from>

    everything worked.

    Before trying this, I think i tried just about everything else, including checking that the correct clientaccesspolicy.xml really was called. The problem was the same weather or not i was doing cross shcema call or not, as long as the silverlight app run in a https context.

    Hope this can help out others facing the same problem.

    Lasse
  28. 3/30/2009 3:31 AM | # re: Silverlight cross domain policy file helpers
    Hi Tim,

    Thanks for the articles, answers a few questions. I'm having problems with a cross-domain call. I have a wcf webservice hosted using an internal dns i.e. mywebservice.domain.com but my site is a media site with the domain www.domain.tv. Running the project locally it's all fine, on the product site no joy.
    I have the clientaccesspolicy as follows but no joy. Get a SecurityException stating it may be due to a cross domain policy error.
    <?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 include-subpaths="true" path="/"/>
    </grant-to>
    </policy>
    </cross-domain-access>
    </access-policy>

    Any help would be appreciated.
    Thanks,
    Michael
  29. 3/30/2009 8:55 AM | # re: Silverlight cross domain policy file helpers
    Michael, you should look at Fiddler or web dev helper to see the actual HTTP response to help you track down the issue.
  30. 4/6/2009 2:05 AM | # re: Silverlight cross domain policy file helpers
    Hi Tim,
    I run into a cross domain problem accessing a wcf service from a Silverlight application. They both run on my workstation (pc211.hoogendoorn.nl). First, I don't understand why I get cross domain errors because they run both on the same domain. Second, I see that the policy file is searched for on port 3014. Here is the output from Fiddler:

    pc211.hoogendoorn.nl/.../...PrototypeTestPage.aspx
    200 OK
    pc211.hoogendoorn.nl/.../GreenBoxPrototype.xap
    200 OK
    pc211.hoogendoorn.nl:3014/clientaccesspolicy.xml
    405 Method Not Allowed
    http://pc211.hoogendoorn.nl:3014/crossdomain.xml
    405 Method Not Allowed

    It looks like there is a bug in the logic that detects two different domains: "pc211.hoogendoorn.nl" and "pc211.hoogendoorn.nl:3014".

    Cheers,
    Leon
  31. 4/6/2009 6:59 AM | # re: Silverlight cross domain policy file helpers
    Leon, if there is an HTTP 405 error, that is the server indicating something is wrong...check if you can just browse to those files via a browser.
  32. 4/13/2009 12:36 PM | # re: Silverlight cross domain policy file helpers
    Hi Tim,

    I am trying to get access to a simple xml file but I do not have control over the source server and there is no crossdomain.xml or clientaccesspolicy.xml. How do I access such a resource?

    Thanks, Justin.
  33. 4/13/2009 12:48 PM | # re: Silverlight cross domain policy file helpers
    Justin -- you don't unless you proxy the call (i.e. host your own service that requests it). See timheuer.com/.../...ithout-cross-domain-files.aspx for some other options
  34. 4/29/2009 8:47 AM | # re: Silverlight cross domain policy file helpers
    Hi Tim,
    Really superb.
  35. Gravatar
    5/11/2009 12:09 PM | # re: Silverlight cross domain policy file helpers
    Hi Tim,

    Thanks for the article. I'm trying to use VS to run both the services and the Silverlight client separately, so they are on a different domain. Is there a way to have VS publish the clientaccesspolicy.xml file to the web root instead of the application root?

    Thanks, Ed
  36. Gravatar
    5/18/2009 5:45 AM | # re: Silverlight cross domain policy file helpers
    Hi Tim, I don't know why this cross-domain error appear

    I have a Silverlight app that consuming a webservice and they work fine, so
    - I'm able to retrieve http://www.mywebsite.com/clientaccesspolicy.xml
    - I'm able to retrive the asmx web service http://www.mywebsite.com/mywebservices/myws.asmx

    Now I'm trying to test an other SL app, on localhost, that simply use the same webservice

    - I have configured the SilverlightApplication1.Web project as a web application
    - I have selected the SilverlightApplication1.Web project as the default
    - I have add the serviceReference to myws.asmx webservice

    but when the SL app runs (localhost:1968/...erlightApplication1TestPage.aspx)

    i am getting following runtime error :

    "An error occurred while trying to make a request to URI 'http://www.mywebsite.com/mywebservices/myws.asmx'.
    This could be due to attempting to access a service in a cross-domain way without a proper cross-domain policy
    in place, or a policy that is unsuitable for SOAP services. You may need to contact the owner of the service
    to publish a cross-domain policy file and to ensure it allows SOAP-related HTTP headers to be sent.
    Please see the inner exception for more details."

    This is the traffic to my app sniffing with Fiddler

    200 localhost:1968 /ClientBin/SilverlightApplication1.xap
    304 www.mywebsite.com /clientaccesspolicy.xml

    Tanks
  37. 5/21/2009 2:30 AM | # re: Silverlight cross domain policy file helpers
    Hi Tim,
    you'r great first of all xD

    ive watched your tutorial and read articles. I am trying to build a website that shows many images. ive added the .XML files and getting data from my host database. its all working on local. but not on web server.

    any idea why ?

    I set up BitmapImages with uri source to "http://mywebsite.com/showpic.aspx?picID="...

    and getting the id of the picture with web service and using outputstream to show image. im new to silverlight, is this way reasonable ?

    as i said, its working fine on local
  38. 5/21/2009 9:03 AM | # re: Silverlight cross domain policy file helpers
    Elroy is there a cross-domain policy in place? if you are using web request methods to get your image, then you'll need that. if you are just setting source properties on Image, you shouldn't. My rule of thumb is look at the traffic, it will tell you what is going on.
  39. 5/21/2009 10:41 AM | # re: Silverlight cross domain policy file helpers
    Hi again
    yes the cross domain policy is in my root, i can see it through www.mypage.com/crossdomain.xml and i tried the client-access also.

    ive just checked with fiddler and it shows this
    Result: 304
    URL: /ClientBin/Silverlightapplication1.xap
    Body: 0

    and it doesnt show the crossdomain.xml and clientaccess...xml

    ty for ur time
  40. 5/21/2009 10:52 AM | # re: Silverlight cross domain policy file helpers
    the .xap's result turned to 200 now. tried loading all again and worked.
    still i cant see the .xml 's in fiddler. is there any iis setting needed ?
    sorry for double post.
  41. 5/21/2009 11:09 AM | # re: Silverlight cross domain policy file helpers
    Doesn't sound like it is x-domain issues then. Take a look in debug mode, add some tests, watch all traffic -- look for the requests to your images and see where it is failing. Hard for me to debug without being over your shoulder.
  42. 5/21/2009 12:19 PM | # re: Silverlight cross domain policy file helpers
    Silverlight app. is supposed to check if crossdomain.xml and clientaccesspolicy.xml exist while loading, isnt it ?

    i checked fiddler on debug and saw anything except those .xml's
    xmls are in my root.
  43. 5/21/2009 12:23 PM | # re: Silverlight cross domain policy file helpers
    Elroy -- no it checks when it makes a x-domain call.
  44. 5/24/2009 1:45 PM | # re: Silverlight cross domain policy file helpers
    Ed -- you'd probably have to give VS some additional post-build commands to copy the file in the right place.
  45. 5/24/2009 1:55 PM | # re: Silverlight cross domain policy file helpers
    GL - is the clientaccesspolicy.xml file *properly configured*?
  46. 6/8/2009 1:20 PM | # re: Silverlight cross domain policy file helpers
    A hard fought lesson I ran into with respect to the crossdomain.xml and clientaccesspolicy.xml files was when accessing SharePoint web services from Silverlight. Not only do these files have to be located in your root web application folder for SharePoint, but you also need to make sure users have READ access to them at the NTFS permission level. Without READ permission, you will get a crossdomain call error.
  47. 6/11/2009 6:05 AM | # re: Silverlight cross domain policy file helpers
    Hi Tim,
    I have a silverlight application which works fine on my local system.But gives me error while hosted in IIS.

    I have a Silverlight-Enabled WCF service in "MyProject.Web" project.A service reference in silverlight project is hosting that WCF.(MyProject.Web and silverlight app are in same solution).Everything works fine on my localsystem.But the application fails in IIS throwing the following exception:
    An error occured while trying to make a request to URI 'http://localhost:2727/Service.svc'.This could be due to attempting to access a service in a cross-domain
    way without a proper cross-domain policy in place, or a policy that is unsuitable for soap services.You may need to contact the owner of the service to publish a
    cross-domain policy file and to ensure ita llows SOAP-related HTTP headers to be sent.Please see the inner exception for more details.


    Can anyone please suggest how to overcome this error.I have added both policy files but I think they are not detected at all.Pls suggest where the files should be added or some other solution.

    Also is there any way to know whether the already added policy files are being detected or not?
  48. 6/22/2009 2:21 PM | # re: Silverlight cross domain policy file helpers
    Hi Tim -

    I've been researching this issue all day, you have a ton of good posts, however, I'm trying to figure out where to put this ClientAccessPolicy.xml file, if I'm working locally and using the visual studio development server. I doubt you'd put it into wwwroot anymore, since thats not being used as the root?

    Maybe I'm dense, just haven't seen anything about this.

    Thanks.
  49. 6/25/2009 1:55 PM | # re: Silverlight cross domain policy file helpers
    Jason -- you're not dense...it's a common question when using the local web dev server. See: timheuer.com/.../...cannot-access-web-service.aspx for an explanation.

 
Please add 1 and 7 and type the answer here:
First time here? You are looking at the most recent posts. You may also want to check out older archives. Please leave a comment, ask a question and consider subscribing to the latest posts via RSS or email. Thank you for visiting! (hide this)