Advertisement

CORS & Cross-Origin Resource Sharing interview questions and answers

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

15 questions
Advertisement

15 questions

Q11 Advanced Save

How do you configure CORS to expose custom response headers to client-side JavaScript using Access-Control-Expose-Headers?

By default, JavaScript running in the browser can only read a small set of 'safe-listed' response headers (like Content-Type, Content-Length) from a cross-origin response -- any custom headers your API returns (like X-Total-Count for pagination) are invisible to client JS unless explicitly exposed via WithExposedHeaders() in the CORS policy, which adds them to the Access-Control-Expose-Headers response header.
builder.Services.AddCors(options => {
    options.AddPolicy("MyPolicy", policy => {
        policy.WithOrigins("https://app.mycompany.com")
              .WithExposedHeaders("X-Total-Count", "X-Page-Number");
    });
});

// Now client JS can read: response.headers.get('X-Total-Count')
Real-world example
A paginated API returning results with a custom X-Total-Count header initially confused frontend developers who saw the header in browser dev tools' network tab but couldn't read it via fetch() in JavaScript -- resolved by adding it to WithExposedHeaders.
Likely follow-ups
  • Why are only a small set of headers readable by default without this configuration?
  • How does this differ for same-origin requests, which have no such restriction?
RESTful Web APIs & Controllers ASP.NET Core Middleware & Request Pipeline
Q12 Intermediate Save

How do you troubleshoot a CORS error reported in browser developer tools, and what does the error message typically indicate?

A typical browser CORS error (like 'No Access-Control-Allow-Origin header is present on the requested resource') means the response lacked the required CORS header entirely -- check whether UseCors was actually registered and positioned correctly in the pipeline, whether the specific origin making the request is included in the policy, and importantly, 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 on the requested resource.

// Debugging steps: check Network tab for the OPTIONS preflight response 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 support tickets?
Debugging & Profiling ASP.NET Core Middleware & Request Pipeline
Q13 Advanced Save

How does CORS behave differently for requests made from a mobile app or server-to-server call 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 (using HttpClient/URLSession), 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: developers sometimes mistakenly believe CORS provides genuine API security, when it's purely a client-side browser convenience/safety mechanism that a non-browser client can simply ignore 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 Authentication & Authorization (Identity JWT OAuth)
Q14 Beginner Save

What does the WithMethods() CORS policy builder method control, and why restrict it instead of using AllowAnyMethod()?

WithMethods() explicitly whitelists which HTTP methods (GET, POST, PUT, DELETE, etc.) are permitted for cross-origin requests under a policy, letting you follow the principle of least privilege by only allowing the specific methods an origin actually needs -- e.g., 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 misconfigured.
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 is 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?
CORS & Cross-Origin Resource Sharing RESTful Web APIs & Controllers
Q15 Intermediate Save

How would you set up a CORS policy that allows any subdomain of a given domain (like *.mycompany.com) using SetIsOriginAllowed?

Since WithOrigins() requires exact origin string matches, allowing a dynamic pattern like any subdomain requires SetIsOriginAllowed(Func<string, bool>), which lets you write custom matching logic (e.g., checking if the origin's host ends with '.mycompany.com') evaluated per-request, rather than needing to enumerate every possible subdomain explicitly in advance.
options.AddPolicy("AnySubdomain", policy => {
    policy.SetIsOriginAllowed(origin => {
        var uri = new Uri(origin);
        return uri.Host.EndsWith(".mycompany.com", StringComparison.OrdinalIgnoreCase);
    }).AllowCredentials().AllowAnyHeader().AllowAnyMethod();
});
Real-world example
A multi-tenant SaaS platform where each customer gets a subdomain (customer1.mycompany.com, customer2.mycompany.com) uses SetIsOriginAllowed to permit any valid tenant subdomain without needing to update the CORS policy every time a new customer signs up.
Likely follow-ups
  • What security risks arise from an overly loose subdomain-matching pattern?
  • How would you validate the pattern doesn't accidentally match an attacker-controlled domain like evilmycompany.com?
CORS & Cross-Origin Resource Sharing Multiple Inheritance & MRO

Showing 11–15 of 15