How to enable CORS for web page access from a different domain in Asp.Net MVC

0

I'm developing an application that is communicating with Secure Paging Checkout through the Web Server link that is working perfectly, after making the payment PagSeguro provides the possibility to indicate a page in my application to receive the transaction status.

I'm developing on localhost, and the IIS-generated address is not a valid address for the PayPal Sandbox link

In the Pagseguro documentation, you are asked to use the CORS feature below

Response.AppendHeader ("Access-Control-Allow-Origin", " link ");

/ strong>

The question how and where do I enter the code in my application is Response.AppendHeader ?

    
asked by anonymous 15.10.2018 / 14:40

1 answer

1

For this you must install an extension package that does this CORS treatment.

In the NUGET console enter the following command:

Install-Package Microsoft.AspNet.WebApi.Cors 

In your WebApiConfig file put the code config.EnableCors(); :

 public static class WebApiConfig 
    { 
        public static void Register(HttpConfiguration config) 
        { 
            // Configurando o CORS
            config.EnableCors(); 


            config.MapHttpAttributeRoutes(); 

            config.Routes.MapHttpRoute( 
                name: "DefaultApi", 
                routeTemplate: "api/{controller}/{id}", 
                defaults: new { id = RouteParameter.Optional } 
            ); 
        } 
    } 

In your controller put the following attribute:

 [EnableCors(origins: "https://sandbox.pagseguro.uol.com.br", headers: "*", methods: "*")] 

or for future calls, considering calls from all sources:

[EnableCors(origins: "*", headers: "*", methods: "*")] 
    
15.10.2018 / 15:39