concept request in category silverlight

appears as: requests, request, requests, request
Silverlight 4 in Action: Silverlight 4, MVVM, and WCF RIA Services

This is an excerpt from Manning's book Silverlight 4 in Action: Silverlight 4, MVVM, and WCF RIA Services.

Layout in Silverlight and WPF involves two primary passes: the measure pass and the arrange pass. In the measure pass, the layout system asks each element to provide its dimensions given a provided available size. In the arrange step, the layout system tells each element its final size and requests that it lay itself out and also lay out its child elements. A full run of measuring and arranging is called a layout pass.

The clientaccesspolicy.xml file defines these policies; it needs to be placed at the root of the domain hosting any web service that’s allowed to be accessed from a different domain. Even if there’s a valid policy file, if it’s located anywhere other than the root of the hosting domain, your application won’t find it, and the request will fail. If the file is in place and has the proper attributes, your application is considered trusted, and the call will return as expected. So, what does a properly formatted policy file look like? Take a look at this example:

<?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>

This shows the minimum needed in a clientaccesspolicy.xml file to allow HTTP access to all web services hosted on the current domain. If you want to have different rights for different services or to allow for socket access to the server, you can make additions to that file. Sockets are described in section 14.5. The example here is as open as possible-requests from any domain can access any resource in the host domain, and host headers are allowed. Table 14.1 shows the elements and attributes that make up a clientaccesspolicy.xml file. Attributes are shown after the element they apply to.

Table 14.1. Elements and attributes allowed in clientaccesspolicy.xml

Element/attribute

Required

Description

access-policy Yes Root element for the policy file.
cross-domain-policy Yes Container for one or more policy elements.
policy Yes Defines rules for a single domain or a group of domains.
allow-from Yes Container for permitted domains. If it contains no domain elements, no cross-domain access is granted.
  http-request-headers No Defines which headers are allowed to be sent to the web services hosted at the current domain. If absent, no headers are allowed.
domain Yes Defines domains affected by the policy element in which the domain is a child.
  uri Yes Specifies the exact domain allowed for the current policy.
grant-to Yes Container for one or more resource elements.
resource Yes Required for WebClient or HttpWebRequest classes. Defines server resources affected by the current policy.
  Path Yes Required for WebClient or HttpWebRequest classes. Identifies individual files or services allowed or denied by the current policy. Format is a URI relative to the root of the domain.
  include-subpaths No Optional for WebClient or HttpWebRequest classes. If absent, subfolder access is denied.
socket-resource Yes Required for socket access. Defines socket resources affected by the current policy.
  Port Yes Required for socket access. Defines a port or range of ports, which must fall between 4502 and 4534, affected by the current policy.
  Protocol Yes Required for socket access. Defines what protocols are allowed under the current policy. The only protocol currently allowed is TCP.

I know that you’re anxious to see how to connect to data from within your application, but you need to create a solid foundation on which to build service access. You can make the policy file as open or as restrictive as you desire. By changing the domain element, you can limit access to a single domain. You can also add multiple policy elements to apply different rules to requests from different domains, as shown in the next example.

Because Silverlight typically uses the browser’s networking stack, cookies for the current domain also get added to requests from your Silverlight application. This is good if you’re hosting your Silverlight component in an authenticated application that uses tokens stored in a cookie. In this case, the token also authenticates the requests originating from your Silverlight application, ensuring that you can access the resources you’re authorized for.

One potential problem using this method is that when a user has a cookie-based token for a given domain, any Silverlight request to that domain contains the auth cookie, even if the request is from a different Silverlight application than the one you intend. Another issue is that this method of authentication relies on the client browser having session cookies enabled—which isn’t always true.

Many endpoints on the Web, and even more on internal networks, are protected by some sort of authentication scheme. In order to access those endpoints, you must be able to provide authentication information along with the request.

The client networking stack supports NTLM, basic, and digest authorization, allowing you to pass credentials to the endpoint of a request via the Credentials property. Listing 14.8 shows how to use credentials with the client networking stack.