GithubHelp home page GithubHelp logo

webapi-cors's Introduction

Toolhouse.WebApi.Cors

This is a small library to help enable CORS support in ASP.NET WebApi applications. Specifically, it makes it relatively easy to enable unrestricted access to an API from one or more origins, optionally using wildcards.

What is CORS?

See MDN documentation.

Enabling CORS

First add a project reference to Toolhouse.WebApi.Cors. Then update Web.config, ensuring there's something like this in the <system.webServer> section:

<system.webServer>
  <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>

(CORS makes extensive use of OPTIONS HTTP method. This change instructs ASP.NET to handle OPTIONS requests, which it won't by default.)

Next, enable CORS support. Example:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Not shown: other configuration

        config.EnableCors(new Toolhouse.WebApi.Cors.ConfigCorsPolicyProvider("MyApp.AllowedOrigins"));

    }
}

Make sure to add the allowed origins to Web.config:

<appSettings>
    <add key="MyApp.AllowedOrigins" value="http://localhost:*, http://*.example.org">
</appSettings>

Internals

This library includes two ICorsPolicyProvider implementations:

  • WildcardCorsPolicyProvider accepts a wildcard expression and automatically enables CORS for any origin that matches.
  • ConfigCorsPolicyProvider reads a wildcard expression from a key in <appSettings>.

Wildcard Support

For specifying origins, we support wildcards (*) for subdomains and ports.

Example Matches Does Not Match
http://localhost:* http://localhost:3000, http://localhost http://foo.localhost
https://*.example.org https://foo.example.org https://foo.bar.example.org

Multiple patterns can be specified by separating them with a comma (,).

webapi-cors's People

Contributors

bgradin avatar

Watchers

Kevin Stock avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.