Enable CORS in OWIN Self Hosted WebApi

2.6k views Asked by At

I am trying to get CORS working in a WebAPI that is self hosted in OWIN. The code I have is

    public void Configuration(IAppBuilder appBuilder)
    {
        var configuration = new HttpConfiguration();

        appBuilder.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
        appBuilder.UseWebApi(configuration);

        configuration.Routes.Add("API Default", new HttpRoute("{Controller}"));

        appBuilder.Run((owinContext) =>
        {
            owinContext.Response.ContentType = "text/plain";
            return owinContext.Response.WriteAsync("Api is available at:  /ReceiptPrinter");
        }); 
    }

Whenever I access this from a secure page I get an insecure error message and the browser blocks the request.

Is it possible to do this?

I have seem answers like this http://rion.io/2013/06/03/using-cors-in-asp-net-webapi-without-being-a-rocket-scientist/ but they don't seem to deal with self hosted apps.

0

There are 0 answers