I have a REST API using OWIN with Web Api 2 Controllers. I need to expose an Authenticate
method but only to requests coming from the webserver hosting an AngularJS application (same host as the REST API), which means as far as I'm aware than I need to disable CORS for that one method only.
An example of what my ApiController
class looks like would be (RequireHttpsAttribute
enforces SSL URL request scheme):
[Authorize]
[RequireHttps]
[RoutePrefix("api/v1")]
public class RestController : ApiController
{
[AllowAnonymous, Route("Authenticate")]
public async Task<IHttpActionResult> Authenticate([FromBody] AuthenticationModel authenticationModel)
{ ... }
[HttpGet, Route("SecureData/{id:int}")]
public async Task<IHttpActionResult> GetSecureData(int id)
{ ... }
[HttpPost, Route("SecureData")]
public async Task<IHttpActionResult> CreateSecureData([FromBody] SecureDataModel data)
{ ... }
[HttpPut, Route("SecureData")]
public async Task<IHttpActionResult> UpdateSecureData([FromBody] SecureDataModel data)
{ ... }
}
The OWIN CORS configuration appears to be a blanket application of policy, via the CorsPolicy
class and app.UseCors(CorsPolicy.AllowAll)
in an OWIN Startup
class.
The resources I have found (including other SO questions such as this one) point to the fact that OWIN CORS and ASP.NET CORS are not compatible with each other, so it doesn't appear that I can simply decorate the method with DisableCorsAttribute
(I also currently don't have any ASP.NET dependencies in my project so I would be adding all the dependencies for that single attribute!).
My question is: How do I disable CORS in OWIN for a single Web Api 2 Controller method?