---
title: "Implementing CORS Policies in .NET Core Web API to Control API Access"  
description: "It enables web applications running on a single domain to access resources from another domain. However, CORS can be a security risk if not configured"  
author: "Sanjay Goenka"  
published: 2023-09-14  
updated: 2023-09-14  
canonical: https://www.mindstick.com/articles/333602/implementing-cors-policies-in-dot-net-core-web-api-to-control-api-access  
category: "web api"  
tags: ["c#", "api(s)", ".net core", ".net core api"]  
reading_time: 3 minutes  

---

# Implementing CORS Policies in .NET Core Web API to Control API Access

#### Introduction

CORS is a fundamental component of contemporary [web development](https://www.mindstick.com/services/web-development). It enables [web applications](https://www.mindstick.com/blog/11464/improve-your-understanding-of-web-applications) running on a single domain to access resources from another domain. However, CORS can be a security risk if not configured properly. This article will provide an overview of how CORS policies can be implemented in .NET Core Web APIs to [effectively manage](https://answers.mindstick.com/qa/114998/can-multilateral-institutions-like-the-un-effectively-manage-21st-century-security-crises) access to the API.

\

#### Understanding CORS

Before we dive into the implementation, let’s take a quick look at what CORS is, why it’s important, and what it’s used for.

**What is CORS →** CORS (Cross-Origin [Resource Sharing](https://www.mindstick.com/forum/159747/what-is-the-purpose-of-cors-cross-origin-resource-sharing-in-a-dot-net-core-api-and-how-do-you-confi)) is a security feature that web browsers implement to manage and protect cross-origin requests that web pages make. By default, web browsers don’t allow web pages to make requests to domains other than their own. This is to [protect against](https://answers.mindstick.com/qa/111696/what-are-the-main-security-threats-in-programming-and-how-can-i-protect-against-them) cross-site [request forgery](https://answers.mindstick.com/qa/112056/how-do-i-prevent-cross-site-request-forgery-csrf-attacks-in-my-web-applications) attacks (CSRF). But there are legitimate uses for cross-origin requests. For example, a [web application](https://www.mindstick.com/interview/1126/does-silverlight-web-application-work-with-all-browsers) may need to make a cross-origin request in order to access resources from a different domain. In such cases, the hosting server can specify which origins can access its resources through CORS policies.

#### Implementing CORS in .NET Core Web API

To implement CORS policies in a .NET Core Web API, follow these steps:

**Step 1: Install the Microsoft.AspNetCore.Cors [NuGet Package](https://www.mindstick.com/forum/155814/what-is-nuget-package-management)**

In your .NET Core Web API project, open the [Package Manager](https://answers.mindstick.com/qa/112022/what-are-the-advantages-of-using-a-package-manager-like-npm-or-yarn-in-javascript-development) Console and run the following command to install the necessary package

```cs
Install-Package Microsoft.AspNetCore.Cors
```

## Step 2: Configure CORS in Startup.cs

In your `Startup.cs` file, locate the `ConfigureServices` and `Configure` methods. Add the following code to configure CORS policies:

```cs
// ConfigureServices method
services.AddCors(options =>
{
    options.AddPolicy("AllowSpecificOrigin", builder =>
    {
        builder
            .WithOrigins("https://example.com", "https://anotherdomain.com")
            .AllowAnyMethod()
            .AllowAnyHeader();
    });
});
```

In the above code, we define a CORS policy called “AllowSpecificOrigin” which permits requests from the specified origins (replacing “example.com” and “anotherdomain.com” with your actual allowed origin). This policy also permits any HTTP method and HTTP header.

Next, add the CORS middleware to the `Configure` method:

```cs
// Configure method
app.UseCors("AllowSpecificOrigin");
```

This code specifies that the "AllowSpecificOrigin" CORS policy should be applied to all incoming requests.

## Step 3: Test Your CORS Configuration

Once you’ve set up CORS, you’ll need to test your implementation to make sure everything is working as it should. You can do this by using browser [developer tools](https://www.mindstick.com/forum/158446/what-is-the-role-of-browser-developer-tools-such-as-chrome-devtools-or-firefox-developer-tools) (such as Postman) to send cross-origin requests to your APIs and check whether CORS is allowing or denying them according to your policy.

#### Conclusion

CORS policies in.NET Core Web APIs are an important part of controlling and securing API access from various domains. By following these steps, you can set up granular control over the origin of API resources while keeping your web application secure. CORS policies are an essential part of building secure and dependable web APIs.

\

\

---

Original Source: https://www.mindstick.com/articles/333602/implementing-cors-policies-in-dot-net-core-web-api-to-control-api-access

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
