Supporting CORS on a RestfulObjects Server

Coordinator
Jul 16, 2012 at 2:02 PM

CORS (http://en.wikipedia.org/wiki/Cross-origin_resource_sharing) is potentially very useful on a Restful Objects server eg allowing a client application to integrate across multiple RO servers. 

You could implement it yourself but we found it very easy to get up and running using the Thinktecture.IdentityModel nuget package.

  1. Add Thinktecture.IdentityModel to the project through nuget.
  2. Add a CorsConfig.cs file under App_Start

     

    using System.Web.Http;
    using Thinktecture.IdentityModel.Http.Cors.WebApi;
    
    public class CorsConfig {
        public static void RegisterCors(HttpConfiguration httpConfig) {
            var corsConfig = new WebApiCorsConfiguration();
    
            // this adds the CorsMessageHandler to the HttpConfiguration's
            // MessageHandlers collection
    
            corsConfig.RegisterGlobal(httpConfig);
    
            // this allows all CORS requests to the RestfulObjects controller
            // from the http://foo.com origin.
    
            corsConfig.ForResources("RestfulObjects").ForOrigins("http://foo.com").AllowAll();
            
        }
    }
    

     

  3.  Hook in config in RestfulObjectsStart.PostStart

 

public static void PostStart() {
           // existing code 
.......

// add CORS CorsConfig.RegisterCors(GlobalConfiguration.Configuration); }
Developer
Jul 17, 2012 at 9:09 AM

On 16 July 2012 22:55, scascarini <notifications@codeplex.com> wrote:

CORS (http://en.wikipedia.org/wiki/Cross-origin_resource_sharing) is potentially very useful on a Restful Objects server eg allowing a client application to integrate across multiple RO servers.

You could implement it yourself but we found it very easy to get up and running using the Thinktecture.IdentityModel nuget package....


Thanks for sharing this, Stef.

I've copied this post into the RO wiki, http://restfulobjects.codeplex.com/documentation

Coordinator
Jul 18, 2012 at 8:45 AM

Thanks, Dan. I've also now added this into the Restful Objects Manual.