How to: Create an HTTP Request Forwarder for External Applications

SharePoint 2010

This topic explains how to create a request forwarding HTTP Request Handler.

Last modified: January 13, 2010

Applies to: SharePoint Foundation 2010

Some applications, such as Silverlight, cannot make cross-domain requests. When the application needs to communicate with a server on another domain, its requests must first be sent to the request-forwarding handler, which, of course, is in the same domain as the application. The handler can then repackage the request and send it to the destination in the external domain.

If a Silverlight Web Part hosts a Silverlight application on a different domain from the SharePoint Foundation Web application whose page has the Web Part, and if the Silverlight application needs to send requests to the Web application, then a request-forwarding handler must be created and installed on the domain of the Silverlight application. The URL of the handler is specified in External Application XML when the Web Part is added to a page. For more information about creating this XML, see How to: Create External Application XML Markup.

Most of the logic needed by the handler is included in the RequestForwarder class. The handler class you create is a kind of wrapper around this class.

To Create a Request Forwarding Handler

  1. Add a plain text file to your Visual Studio project and give it an .ashx extension.

  2. Add a WebHandler directive to the top of the page. Use the Language and Class attributes to specify the programming language and the class that will implement the handler. The following is an example.

    <%@ WebHandler Language="C#" Class="ToSharePointForwarder" %>
  3. Below this directive, you add code exactly as you would in an ordinary code file. Begin by adding using statements, including one for the Microsoft.SharePoint.Client namespace.

    using System;
    using System.Web;
    using Microsoft.SharePoint.Client;
  4. Add a declaration for your handler class. Since it is an HTTP handler, it must implement the IHttpHandler interface.

    public class ToSharePointForwarder : IHttpHandler 
  5. Add an implementation of the IsReusable property. It should simply return false.

    public bool IsReusable 
        get { return false; }
  6. Add an implementation of the ProcessRequest(HttpContext) method. For the most part, your implementation is simply a wrapper around the ProcessRequest() method of the RequestForwarder class. However, you must first add the credentials of the application principal to the request. In this example, a NetworkCredential is constructed from the logon name and password of the application principal user and the Active Directory domain of that user.

    public void ProcessRequest (HttpContext context) 
        RequestForwarder forwarder = new RequestForwarder(context);
        if (!String.IsNullOrEmpty(forwarder.Url))
        {        forwarder.WebRequest.Credentials = new System.Net.NetworkCredential("ContosoSilverlightApp", "&Tu*)2v", "Contoso");