Advertisement

CORS (Cross-Origin Resource Sharing) interview questions and answers

15 CORS (Cross-Origin Resource Sharing) interview questions for ASP.NET Core roles, each with a model answer, examples and likely follow-ups.

15 questions
Advertisement

15 questions

Q11 Advanced Save

How does CORS behave for requests made from a mobile app, server-to-server call, or a tool like curl/Postman, compared to a browser-based web app?

CORS is exclusively a browser-enforced restriction -- it has no effect whatsoever on non-browser HTTP clients like mobile apps, server-to-server calls, or tools like curl/Postman, since there's no browser present to enforce the same-origin policy or send preflight requests -- this is a common point of confusion, since CORS is purely a client-side browser convenience/safety mechanism, not genuine API security, and a non-browser client can simply ignore it entirely.
Example

// This works perfectly fine from a mobile app or curl, with ZERO CORS restrictions: // curl https://api.mycompany.com/orders -H "Authorization: Bearer xyz" // No preflight, no Origin header check -- CORS simply doesn't apply outside a browser

Real-world example
A security review corrects a misconception that a strict CORS policy was 'protecting' an API, clarifying that anyone using Postman, curl, or a mobile app can call the API directly with zero CORS restrictions -- real authorization checks are the actual security boundary, not CORS.
Likely follow-ups
  • What IS the actual security boundary if CORS doesn't protect against non-browser clients?
  • Why do developers commonly conflate CORS with API security?
RESTful Web APIs & Controllers Authorization
Q12 Intermediate Save

How would you troubleshoot a CORS error in browser developer tools, and what's the most common root cause?

A typical CORS error ('No Access-Control-Allow-Origin header is present') means the response lacked the required header entirely -- check whether UseCors was registered and positioned correctly in the pipeline, whether the requesting origin is actually included in the policy, and critically, check the Network tab for the actual preflight OPTIONS response (not just the failed real request), since preflight failures are the most common root cause and manifest confusingly on the subsequent real request instead.
// Browser console error:
// Access to fetch at 'https://api.mycompany.com/orders' from origin
// 'https://app.mycompany.com' has been blocked by CORS policy:
// No 'Access-Control-Allow-Origin' header is present

// Debugging steps: check the OPTIONS preflight response in Network tab first,
// verify UseCors() is registered and the origin is in the allowed list
Real-world example
A developer initially assumes their API code itself is broken due to a CORS error, but checking the Network tab reveals the actual failure is the OPTIONS preflight request returning a 404, since UseCors was never actually registered in the middleware pipeline.
Likely follow-ups
  • Why do CORS errors appear as generic network failures rather than specific HTTP error codes?
  • What common misconfigurations cause the most CORS-related support tickets?
Diagnostics & Performance ASP.NET Core Middleware & Request Pipeline
Q13 Advanced Save

How do you configure CORS to allow multiple specific known origins simultaneously, such as production, staging, and local development?

WithOrigins() accepts multiple origin strings as parameters (or SetIsOriginAllowed for more dynamic matching), letting a single policy explicitly whitelist several known, trusted origins simultaneously without resorting to an insecure wildcard, commonly pulling the actual list from configuration rather than hardcoding it so different environments can have different allowed origins.
var allowedOrigins = builder.Configuration.GetSection("Cors:AllowedOrigins").Get<string[]>();

builder.Services.AddCors(options => {
    options.AddPolicy("KnownFrontends", policy => {
        policy.WithOrigins(allowedOrigins).AllowAnyMethod().AllowAnyHeader();
    });
});
Real-world example
A team maintains one CORS policy listing their production domain, staging domain, and localhost development URL together, pulled from configuration so each deployed environment can have a different, environment-appropriate allowed origins list.
Likely follow-ups
  • Should localhost origins ever be included in a production CORS configuration?
  • How do you manage this origin list securely across different deployment environments?
Configuration & Options Pattern CI/CD Publishing & Deployment
Q14 Intermediate Save

How does the WithMethods() CORS policy builder method restrict cross-origin requests, and why is it preferable to AllowAnyMethod() for sensitive endpoints?

WithMethods() explicitly whitelists which HTTP methods (GET, POST, PUT, DELETE) are permitted for cross-origin requests under a policy, following the principle of least privilege -- a read-only public API can restrict cross-origin access to GET only, preventing any cross-origin attempt at POST/PUT/DELETE even if other parts of the CORS policy were accidentally misconfigured too permissively.
options.AddPolicy("ReadOnlyPublic", policy => {
    policy.WithOrigins("https://partner-site.com")
          .WithMethods("GET")  // only GET allowed cross-origin, not POST/PUT/DELETE
          .AllowAnyHeader();
});
Real-world example
A public read-only reporting API restricts its CORS policy to WithMethods("GET") specifically so that even if a partner site's origin configuration were somehow compromised, cross-origin write operations remain impossible regardless of any other misconfiguration.
Likely follow-ups
  • How does this interact with preflight requests for methods not in the allowed list?
  • Should WithMethods generally be preferred over AllowAnyMethod in production APIs?
RESTful Web APIs & Controllers Rate Limiting
Q15 Advanced Save

How would you write an integration test verifying that a CORS policy correctly allows a specific origin and rejects an unauthorized one?

Using WebApplicationFactory, send a request with an explicit Origin header set to both an allowed and a disallowed origin, then assert on the presence (or absence) of the Access-Control-Allow-Origin response header, verifying the actual CORS behavior end-to-end rather than just trusting the policy configuration is correct by inspection alone.
[Fact]
public async Task Cors_AllowsConfiguredOrigin() {
    var client = _factory.CreateClient();
    var request = new HttpRequestMessage(HttpMethod.Get, "/api/products");
    request.Headers.Add("Origin", "https://app.mycompany.com");
    var response = await client.SendAsync(request);
    Assert.Equal("https://app.mycompany.com", response.Headers.GetValues("Access-Control-Allow-Origin").First());
}
Real-world example
A CI pipeline runs automated CORS policy tests confirming the production allowlist correctly permits the actual deployed frontend origin and rejects a deliberately unauthorized test origin, catching configuration regressions before they reach production.
Likely follow-ups
  • How would you test the preflight OPTIONS request behavior specifically, not just the actual GET/POST response?
  • What's the risk of only manually verifying CORS configuration instead of automated tests?
Testing ASP.NET Core Applications CI/CD Publishing & Deployment

Showing 11–15 of 15