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

Asked

Viewed 795 times

1

I am developing an application that is communicating with Secure Pag Checkout through the Web Server url https://ws.sandbox.pagseguro.uol.com.br that is working perfectly, after making the payment Pagseguro provides the possibility to indicate a page in my application to receive the status of the transaction.

Only I’m developing on localhost and the address generated by IIS is not a valid address for Pagseguro’s Sandbox http://localhost:12345/

In the Pagseguro documentation you ask to use the CORS resource below

Response.Appendheader("Access-Control-Allow-Origin", "https://sandbox.pagseguro.uol.com.br");

The question how and where I insert this configuration into my application code Response.Appendheader ?

1 answer

1


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

In the NUGET console type 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: "*")] 
  • I performed all the above procedures and also put in Pagseguro the return URL of my application " http://localhost/Announcements/Feedback " and it doesn’t work, what I’m doing wrong.

  • Insert row Enablecors here ... [Enablecors(Rigins: "https://sandbox.pagseguro.uol.com.br", headers: "", methods: "")] public class Adverticioscontroller : Controller{ ... }

  • Error persists as "cross-origin"?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.