From the course: Advanced ASP.NET Web API 2.2
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Creating a custom CORS policy
From the course: Advanced ASP.NET Web API 2.2
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.