Home » c# » c# – CORS Error in Angular BotDetect with ASP.NET WebApi2 backend-Exceptionshub

c# – CORS Error in Angular BotDetect with ASP.NET WebApi2 backend-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I use Botdetect in angular 8 project and ASPNET WebApi2 Backend.

but this error

Access to XMLHttpRequest at ‘http://localhost:29739/simple-captcha-endpoint.ashx?get=html&c=yourFirstCaptchaStyle‘ from origin ‘http://localhost:4500‘ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

I use EnableCors

 config.EnableCors();
        config.EnableCors(new EnableCorsAttribute("*", "*", "*")
        {
            SupportsCredentials = true,

        });

and added botdetect.xml file

<captchaEndpoint>
 <accessControlAllowOriginHeaderEnabled>true</accessControlAllowOriginHeaderEnabled>
 <accessControlAllowOriginHeaderValue>http://localhost:4500</accessControlAllowOriginHeaderValue>
</captchaEndpoint>
How to&Answers:

If you work on local application, you can just install extension.

If you want a real solution you have to add a proxy config, please check this link

This happens becuase your front and back have not the same URL and port, it’s a cross-domain request.