In a recent post I’d written about my struggles with the Web API CORS support and in the comments Taiseer suggested looking at accomplishing it through OWIN. I needed to create a new Web API project this week and so tried out this approach – the short version is it was utterly painless.
This project is (sorry!) closed source / commercial so I can’t share the source directly however it was an easy set of steps to enable CORS support through OWIN on my Web API project.
1. Either through the Package Manager GUI or console install the package Microsoft.Owin.Cors into your Web API project.
2. Within one of your startup files (easiest one out the box is Startup.Auth.cs and that seemed to be a reasonable place for enabling CORS) just add the line:
And that’s it.
If you want finer grain support over what domains and verbs are allowed you can supply detailed configuration via a the CorsOptions class.