From the course: Advanced ASP.NET Web API 2.2

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Creating a custom CORS policy

Creating a custom CORS policy

From the course: Advanced ASP.NET Web API 2.2

Start my 1-month free trial

Creating a custom CORS policy

- [Instructor] We have seen that we can use a default enable CORS attribute to enable the CORS policy but in .NET, we can even create our own custom CORS policies. So let us go to Visual Studio and create our own custom CORS policy. In here, go inside the WebApi. dependency injection project and create a new folder. So Add, New Folder. We are going to name this folder CustomPolicies. Then right click and go to Add, Class. Let us name this class CustomCorsPolicyAttribute and to create custom CORS attributes, we need to inherit from the Attribute, and the ICorsPolicyProvider. Let us import the necessary namespace. And then implement the interface. So in here we see that the ICorsPolicyProvider has a method named GetCorsPolicyAsync which takes two parameters, a request of type HTTP request message and a cancellationToken. So now let us start defining our policy. Let's write in here private CorsPolicy _policy. Let…

Contents