Skip to content

Commit 11c66ec

Browse files
committed
PR feedback
1 parent 401f19c commit 11c66ec

File tree

5 files changed

+989
-932
lines changed

5 files changed

+989
-932
lines changed

src/Middleware/CORS/samples/SampleDestination/Program.cs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,36 @@ public class Program
1212
{
1313
public static void Main(string[] args)
1414
{
15-
Type startupType = null;
16-
17-
var startup = Environment.GetEnvironmentVariable("CORS_STARTUP");
18-
switch (startup)
19-
{
20-
case "Startup":
21-
startupType = typeof(Startup);
22-
break;
23-
case "StartupWithoutEndpointRouting":
24-
startupType = typeof(StartupWithoutEndpointRouting);
25-
break;
26-
}
27-
28-
if (startupType == null)
29-
{
30-
throw new InvalidOperationException("Could not resolve the startup type. Unexpected CORS_STARTUP environment variable.");
31-
}
32-
3315
var host = new WebHostBuilder()
3416
.UseKestrel()
3517
.UseUrls("http://+:9000")
3618
.UseContentRoot(Directory.GetCurrentDirectory())
3719
.ConfigureLogging(factory => factory.AddConsole())
38-
.UseStartup(startupType)
20+
.UseStartup(GetStartupType())
3921
.Build();
4022

4123
host.Run();
4224
}
25+
26+
private static Type GetStartupType()
27+
{
28+
var startup = Environment.GetEnvironmentVariable("CORS_STARTUP");
29+
if (startup == null)
30+
{
31+
return typeof(Startup);
32+
}
33+
else
34+
{
35+
switch (startup)
36+
{
37+
case "Startup":
38+
return typeof(Startup);
39+
case "StartupWithoutEndpointRouting":
40+
return typeof(StartupWithoutEndpointRouting);
41+
}
42+
}
43+
44+
throw new InvalidOperationException("Could not resolve the startup type. Unexpected CORS_STARTUP environment variable.");
45+
}
4346
}
4447
}

src/Middleware/CORS/src/Infrastructure/CorsEndpointConventionBuilderExtensions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,17 @@
77

88
namespace Microsoft.AspNetCore.Builder
99
{
10+
/// <summary>
11+
/// CORS extension methods for <see cref="IEndpointConventionBuilder"/>.
12+
/// </summary>
1013
public static class CorsEndpointConventionBuilderExtensions
1114
{
15+
/// <summary>
16+
/// Adds a CORS policy with the specified name to the endpoint(s).
17+
/// </summary>
18+
/// <param name="builder">The endpoint convention builder.</param>
19+
/// <param name="policyName">The CORS policy name.</param>
20+
/// <returns>The original convention builder parameter.</returns>
1221
public static IEndpointConventionBuilder WithCorsPolicy(this IEndpointConventionBuilder builder, string policyName)
1322
{
1423
if (builder == null)

src/Middleware/CORS/src/Infrastructure/CorsMiddleware.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ namespace Microsoft.AspNetCore.Cors.Infrastructure
1616
/// </summary>
1717
public class CorsMiddleware
1818
{
19+
// Property key is used by MVC filters to check if CORS middleware has run
20+
private const string CorsMiddlewareInvokedKey = "__CorsMiddlewareInvoked";
21+
1922
private readonly Func<object, Task> OnResponseStartingDelegate = OnResponseStarting;
2023
private readonly RequestDelegate _next;
2124
private readonly CorsPolicy _policy;
@@ -132,6 +135,9 @@ private async Task InvokeCore(HttpContext context, ICorsPolicyProvider corsPolic
132135
// fetch policy by name, prioritizing it above policy on middleware
133136
// 3. If there is no policy on middleware then use name on middleware
134137

138+
// Flag to indicate to other systems, e.g. MVC, that CORS middleware was run for this request
139+
context.Items[CorsMiddlewareInvokedKey] = true;
140+
135141
var endpoint = context.GetEndpoint();
136142

137143
// Get the most significant CORS metadata for the endpoint

0 commit comments

Comments
 (0)