Skip to content

Commit 8c3100c

Browse files
UseCORS/before/UseResponseCaching (#18910)
* UseCORS/before/UseResponseCaching * work * work * work * work
1 parent d0ed82b commit 8c3100c

File tree

6 files changed

+20
-6
lines changed

6 files changed

+20
-6
lines changed

aspnetcore/fundamentals/middleware/index.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ The following `Startup.Configure` method adds security-related middleware compon
8080
In the preceding code:
8181

8282
* Middleware that is not added when creating a new web app with [individual users accounts](xref:security/authentication/identity) is commented out.
83-
* Not every middleware needs to go in this exact order, but many do. For example, `UseCors`, `UseAuthentication`, and `UseAuthorization` must go in the order shown.
83+
* Not every middleware needs to go in this exact order, but many do. For example:
84+
* `UseCors`, `UseAuthentication`, and `UseAuthorization` must go in the order shown.
85+
* `UseCors` currently must go before `UseResponseCaching` due to [this bug](https://github.com/dotnet/aspnetcore/issues/23218).
8486

8587
The following `Startup.Configure` method adds middleware components for common app scenarios:
8688

@@ -237,7 +239,7 @@ ASP.NET Core ships with the following middleware components. The *Order* column
237239
| [Authentication](xref:security/authentication/identity) | Provides authentication support. | Before `HttpContext.User` is needed. Terminal for OAuth callbacks. |
238240
| [Authorization](xref:Microsoft.AspNetCore.Builder.AuthorizationAppBuilderExtensions.UseAuthorization*) | Provides authorization support. | Immediately after the Authentication Middleware. |
239241
| [Cookie Policy](xref:security/gdpr) | Tracks consent from users for storing personal information and enforces minimum standards for cookie fields, such as `secure` and `SameSite`. | Before middleware that issues cookies. Examples: Authentication, Session, MVC (TempData). |
240-
| [CORS](xref:security/cors) | Configures Cross-Origin Resource Sharing. | Before components that use CORS. |
242+
| [CORS](xref:security/cors) | Configures Cross-Origin Resource Sharing. | Before components that use CORS. `UseCors` currently must go before `UseResponseCaching` due to [this bug](https://github.com/dotnet/aspnetcore/issues/23218).|
241243
| [Diagnostics](xref:fundamentals/error-handling) | Several separate middlewares that provide a developer exception page, exception handling, status code pages, and the default web page for new apps. | Before components that generate errors. Terminal for exceptions or serving the default web page for new apps. |
242244
| [Forwarded Headers](xref:host-and-deploy/proxy-load-balancer) | Forwards proxied headers onto the current request. | Before components that consume the updated fields. Examples: scheme, host, client IP, method. |
243245
| [Health Check](xref:host-and-deploy/health-checks) | Checks the health of an ASP.NET Core app and its dependencies, such as checking database availability. | Terminal if a request matches a health check endpoint. |
@@ -247,7 +249,7 @@ ASP.NET Core ships with the following middleware components. The *Order* column
247249
| [HTTP Strict Transport Security (HSTS)](xref:security/enforcing-ssl#http-strict-transport-security-protocol-hsts) | Security enhancement middleware that adds a special response header. | Before responses are sent and after components that modify requests. Examples: Forwarded Headers, URL Rewriting. |
248250
| [MVC](xref:mvc/overview) | Processes requests with MVC/Razor Pages. | Terminal if a request matches a route. |
249251
| [OWIN](xref:fundamentals/owin) | Interop with OWIN-based apps, servers, and middleware. | Terminal if the OWIN Middleware fully processes the request. |
250-
| [Response Caching](xref:performance/caching/middleware) | Provides support for caching responses. | Before components that require caching. |
252+
| [Response Caching](xref:performance/caching/middleware) | Provides support for caching responses. | Before components that require caching. `UseCORS` must come before `UseResponseCaching`.|
251253
| [Response Compression](xref:performance/response-compression) | Provides support for compressing responses. | Before components that require compression. |
252254
| [Request Localization](xref:fundamentals/localization) | Provides localization support. | Before localization sensitive components. |
253255
| [Endpoint Routing](xref:fundamentals/routing) | Defines and constrains request routes. | Terminal for matching routes. |

aspnetcore/fundamentals/middleware/index/snapshot/StartupAll3.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
5353
app.UseAuthentication();
5454
app.UseAuthorization();
5555
// app.UseSession();
56+
// app.UseResponseCaching();
5657

5758
app.UseEndpoints(endpoints =>
5859
{

aspnetcore/performance/caching/middleware.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ In `Startup.ConfigureServices`, add the Response Caching Middleware to the servi
2929

3030
Configure the app to use the middleware with the <xref:Microsoft.AspNetCore.Builder.ResponseCachingExtensions.UseResponseCaching*> extension method, which adds the middleware to the request processing pipeline in `Startup.Configure`:
3131

32-
[!code-csharp[](middleware/samples/3.x/ResponseCachingMiddleware/Startup.cs?name=snippet2&highlight=16)]
32+
[!code-csharp[](middleware/samples/3.x/ResponseCachingMiddleware/Startup.cs?name=snippet2&highlight=17)]
33+
34+
> [!WARNING]
35+
> <xref:Owin.CorsExtensions.UseCors%2A> must be called before <xref:Microsoft.AspNetCore.Builder.ResponseCachingExtensions.UseResponseCaching%2A> when using [CORS middleware](xref:security/cors).
3336
3437
The sample app adds headers to control caching on subsequent requests:
3538

aspnetcore/performance/caching/middleware/samples/3.x/ResponseCachingMiddleware/Startup.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
3030
}
3131

3232
app.UseStaticFiles();
33-
3433
app.UseRouting();
34+
// UseCors must be called before UseResponseCaching
35+
// app.UseCors("myAllowSpecificOrigins");
3536

3637
app.UseResponseCaching();
3738

aspnetcore/security/cors.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ There are three ways to enable CORS:
5353

5454
Using the [[EnableCors]](#attr) attribute with a named policy provides the finest control in limiting endpoints that support CORS.
5555

56+
> [!WARNING]
57+
> <xref:Owin.CorsExtensions.UseCors%2A> must be called before <xref:Microsoft.AspNetCore.Builder.ResponseCachingExtensions.UseResponseCaching%2A> when using `UseResponseCaching`.
58+
5659
Each approach is detailed in the following sections.
5760

5861
<a name="np"></a>
@@ -61,14 +64,15 @@ Each approach is detailed in the following sections.
6164

6265
CORS Middleware handles cross-origin requests. The following code applies a CORS policy to all the app's endpoints with the specified origins:
6366

64-
[!code-csharp[](cors/3.1sample/Cors/WebAPI/Startup.cs?name=snippet&highlight=3,9,31)]
67+
[!code-csharp[](cors/3.1sample/Cors/WebAPI/Startup.cs?name=snippet&highlight=3,9,32)]
6568

6669
The preceding code:
6770

6871
* Sets the policy name to `_myAllowSpecificOrigins`. The policy name is arbitrary.
6972
* Calls the <xref:Microsoft.AspNetCore.Builder.CorsMiddlewareExtensions.UseCors*> extension method and specifies the `_myAllowSpecificOrigins` CORS policy. `UseCors` adds the CORS middleware. The call to `UseCors` must be placed after `UseRouting`, but before `UseAuthorization`. For more information, see [Middleware order](xref:fundamentals/middleware/index#middleware-order).
7073
* Calls <xref:Microsoft.Extensions.DependencyInjection.CorsServiceCollectionExtensions.AddCors*> with a [lambda expression](/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions). The lambda takes a <xref:Microsoft.AspNetCore.Cors.Infrastructure.CorsPolicyBuilder> object. [Configuration options](#cors-policy-options), such as `WithOrigins`, are described later in this article.
7174
* Enables the `_myAllowSpecificOrigins` CORS policy for all controller endpoints. See [endpoint routing](#ecors) to apply a CORS policy to specific endpoints.
75+
* When using [Response Caching Middleware](xref:performance/caching/middleware), call <xref:Owin.CorsExtensions.UseCors%2A> before <xref:Microsoft.AspNetCore.Builder.ResponseCachingExtensions.UseResponseCaching%2A>.
7276

7377
With endpoint routing, the CORS middleware **must** be configured to execute between the calls to `UseRouting` and `UseEndpoints`.
7478

aspnetcore/security/cors/3.1sample/Cors/WebAPI/Startup.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public void ConfigureServices(IServiceCollection services)
2525
});
2626
});
2727

28+
// services.AddResponseCaching();
2829
services.AddControllers();
2930
}
3031
#endregion
@@ -42,6 +43,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
4243

4344
app.UseCors(MyAllowSpecificOrigins);
4445

46+
// app.UseResponseCaching();
47+
4548
app.UseAuthorization();
4649

4750
app.UseEndpoints(endpoints =>

0 commit comments

Comments
 (0)