By default, Silverlight supports calls to Web services on the same domain or site of origin. Same domain means that calls must use the same sub domain, protocol, and port. This is for security reasons and prevents cross-domain forgery.
The following illustration shows examples of calls that are allowed and not allowed in a Silverlight application that uses the default settings.
Allowed HTTP Communication for Default Settings
.png)
Enabling Cross-Domain Communication by Using Cross-Domain Policy Files
You can enable Web services to be called by a Silverlight application in another domain by deploying a Web service that uses the correct cross-domain policy file at the root of the domain. Silverlight supports two types of cross-domain policy files.
The following illustration shows an example of cross-domain communication by using a client access policy file.
Cross-Domain Communication by using a Client access Policy File
.png)
In general, when a Silverlight application detects that its request is a cross-domain request, it will first look for the Silverlight client access policy file (clientaccesspolicy.xml) at the application root of the Web service. If this request causes a 404 Not Found or other error, the application will then look for the Adobe Flash cross-domain policy file (crossdomain.xml) at the application root. Redirects for the cross-domain policy file are not allowed. In addition, the cross-domain policy file is requested only once per application session.
The following table lists example request Uniform Resource Identifiers (URIs) and where the Silverlight application will look for the cross-domain policy file.
Request URI | Cross-Domain Policy File Location |
|---|
http://contoso.com/services/data | http://contoso.com/clientaccesspolicy.xml |
http://sales.contoso.com/services/data | http://sales.contoso.com/clientaccesspolicy.xml |
http://contoso.com:8080/services/data | http://contoso.com:8080/clientaccesspolicy.xml |
https://contoso.com/services/data | https://contoso.com/clientaccesspolicy.xml |
As mentioned previously, redirects on cross-domain policy files are not allowed. However, a Silverlight application will follow a redirect for a target resource. The resource can be retrieved only if access is granted by the following:
Security Considerations for Web Services Exposed to Silverlight Cross-Domain Callers
There are important security considerations before you allow Silverlight clients to access Web services in a cross-domain situation. Whenever you put a client access policy file in place, you should configure your Web server hosting the Web services to disable browser caching. This enables you to easily update the file or restrict access to your Web services if necessary. Once the client access policy file is checked, it remains in effect for the browser session so the impact of non-caching to the end-user is minimal.
If you provide HTTP and HTTPS sites for your domain, you should provide a client access policy specific to each protocol for the domain. In the client access policy file, you should only enable calls from the same protocol as the hosting domain. For example, if your domain is https://contoso.com, your policy file should only enable Silverlight clients using the HTTPS protocol like the following example.
<allow-from http-request-headers="*">
<domain uri="https://fabrikam.com"/>
<domain uri="https://www.adatum.com"/>
</allow-from>
To prevent malicious attacks, you should never provide one client access policy file for HTTP and HTTPS versions of your domain that enable calls from both HTTP and HTTPS clients. For example, you should never do the following in a single client access policy file.
<!--DO NOT DO THIS-->
<allow-from http-request-headers="*">
<domain uri="http://fabrikam.com"/>
<domain uri="https://fabrikam.com"/>
</allow-from>
In addition, all Silverlight requests are sent with cookies and authentication. This means that if you have Web services that allow users to access private information, you should host these in a different domain than the Web services exposed to third-party callers. For example, consider that you have a Web store hosted at http://www.tailspintoys.com. Your site allows customers to store billing information that includes credit card numbers. You should not expose a Web service that returns product inventory to third-party Silverlight clients at the same domain. Because cookies and authentication are sent with each message, if you host these Web services on the same domain, you have effectively given the third-party callers access to your customer's private billing information. In this example, your publicly exposed Web services could safely be hosted at http://services.tailspintoys.com, because this is a different domain. You must carefully consider who you have exposed Web services to, and what other Web services are located at that domain. Also, you should always keep your client access policy file as restrictive as possible. For more information about exposing secure Web services, see Security Considerations for Service Access and Making a Service Available Across Domain Boundaries.
Client-Access Policy File Example
The Silverlight client access policy file is an XML file that has a simple format. The following example shows a Silverlight client access policy file. This example has two policy entries.
The first policy enables:
The second policy enables:
All method requests from the www.contoso.com domain to the services path. These callers must be using Silverlight 3 or later and must be client HTTP handling to make method calls other than GET and POST.
Does not allow headers on any calls
<?xml version="1.0" encoding="utf-8"?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="SOAPAction">
<domain uri="*"/>
</allow-from>
<grant-to>
<resource path="/services/" include-subpaths="true"/>
</grant-to>
</policy>
<policy >
<allow-from http-methods="*">
<domain uri="http://www.contoso.com"/>
</allow-from>
<grant-to>
<resource path="/services/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
A file may contain as many policy entries as necessary for a particular application root. Calls are allowed per a union of all the policy entries in the file. In this example, if a POST request including a SOAPAction header came from www.contoso.com, it would be allowed per the first policy. For more information on the format of the client access policy file, see Network Security Access Restrictions in Silverlight.
WildCard Character in the Silverlight Cross-Domain Policy File
The Silverlight client access policy file (clientaccesspolicy.xml) can contain a wildcard character (*). The wildcard character can be used to give all domains access to your services, but in general you should limit cross-domain access to a minimal set of domains that you know will require access. In addition, how you use the wildcard character affects communication between HTTP and HTTPS, or cross-scheme, callers, and services.
The following table lists the possible values for the domain node and the effect each value has during cross-scheme access.
URI | HTTPS | HTTP |
|---|
<domain uri = "*"> | Enables all HTTPS callers | Enables all HTTP and HTTPS callers |
<domain uri= "http://*"> | Enables all HTTP callers | Enables all HTTP callers |
<domain uri = "http://www.contoso.com" > | Callers from http://www.contoso.com only | Callers from http://www.contoso.com only |
<domain uri = https://www.contoso.com > | Callers from https://www.contoso.com only | Callers from https://www.contoso.com only |
In addition, you can use the wildcard character to enable callers from specific sub-domains in the <AllowFrom> section of the client access policy file. The following example shows an excerpt of Silverlight client access policy file that uses the wildcard character to enable requests from a sub domain.
<?xml version="1.0" encoding="utf-8"?>
<access-policy>
<cross-domain-access>
<policy >
<allow-from http-request-headers="SOAPAction">
<domain uri="http://*.tailspintoys.com"/>
</allow-from>
...
Method Support for Cross-Domain Requests
You can enable additional HTTP methods for Silverlight applications by using client HTTP handling. You do this by setting the http-methods attribute to the wildcard character (*). If a Silverlight application is using browser HTTP handling, only GET and POST are enabled, regardless of the setting in the client access policy file. Some methods are restricted for security reasons.
<?xml version="1.0" encoding="utf-8"?>
<access-policy>
<cross-domain-access>
<policy >
<allow-from http-methods="*">
<domain uri="http://*.tailspintoys.com"/>
</allow-from>
...
Header Support for Cross-Domain Requests
With browser HTTP handling, request headers are permitted on cross-domain POST requests only when explicitly permitted in the client access policy file. Client HTTP handling enables request headers on all HTTP methods when permitted in the client access policy file. An exception to this rule is Content-Type headers. Content-Type headers are always allowed on cross-domain POST requests and do not need to be explicitly called in the client access policy file. You can also enable additional headers by setting http-request-headers to one of the following:
A single header type (SOAPAction)
A comma separated list of headers (SOAPAction, x-custom-header)
The wildcard character to enable all headers (*)
Note: |
|---|
For security reasons, Silverlight blocks SOAPAction headers that contain Universal Plug and Play (UPnP) instructions. |
The following table summarizes the header support for client access requests.
HTTP Method | Same-Domain calls | Cross-Domain calls |
|---|
GET | Headers always allowed | Allowed with client HTTP handling only. Must be allowed per client access policy file. |
POST | Headers always allowed | Request headers are allowed per client access policy file |
Other methods | Headers always allowed | Client HTTP handling only. Must be allowed per client access policy file. |
Note: |
|---|
The Mozilla Firefox browser does not currently set the Referer header on HTTP GET requests. As a result, a Silverlight application hosted in Firefox does not support the Referer header being set on any GET requests. |
Path Restrictions for Cross-Domain Requests
In addition to requiring a client access policy file, there are restrictions on the characters allowed in the path section of a cross-domain URI that is callable by a Silverlight application, if all paths and sub paths are not allowed. The following illustration shows the path portion of a URI.
Path Portion of a URI
.png)
The following characters are never allowed in a cross-domain request path when paths are restricted by the client access policy file.
For more information about the format of client access policy files and scenarios they enable, see Network Security Access Restrictions in Silverlight and Making a Service Available Across Domain Boundaries.
Adobe Flash Cross-Domain Policy File
Silverlight supports a subset of the Adobe Flash cross-domain policy file (named crossdomain.xml). Silverlight supports the <allow-access-from> tag with the following attributes.
The domain attribute. The only value that is permitted for this attribute is "*", which means that the entire domain is exposed to all callers.
The secure attribute. This attribute is relevant only when the target domain is HTTPS. This attribute accepts a value of true or false, which indicates whether HTTP callers are allowed to access resources within this domain.
The headers attribute. This attribute lists accepted headers.
The following is an example of an Adobe Flash cross-domain policy file (crossdomain.xml) supported by Silverlight.
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM
"http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-access-from domain="*"/ headers="SOAPAction"
secure="true">
</cross-domain-policy>