Skip to content

Commit bb2ca9d

Browse files
committed
feat: Add Options Verb Handler to work with preflight requests
1 parent 824a62d commit bb2ca9d

4 files changed

Lines changed: 57 additions & 1 deletion

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Microsoft.AspNetCore.Http;
2+
using System.Threading.Tasks;
3+
4+
namespace ReferenceProject.Middleware
5+
{
6+
// https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
7+
// https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#Preflighted_requests
8+
// https://developer.mozilla.org/ru/docs/Web/HTTP/Methods/OPTIONS
9+
public class OptionsVerbMiddleware
10+
{
11+
RequestDelegate Next { get; }
12+
13+
public OptionsVerbMiddleware(RequestDelegate next)
14+
{
15+
Next = next;
16+
}
17+
18+
public Task InvokeAsync(HttpContext context)
19+
{
20+
if (context.Request.Method == "OPTIONS")
21+
{
22+
context.Response.StatusCode = StatusCodes.Status200OK;
23+
context.Response.Headers.Add("Access-Control-Allow-Methods", "*");
24+
return Task.CompletedTask;
25+
}
26+
return Next.Invoke(context);
27+
}
28+
}
29+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Microsoft.AspNetCore.Builder;
2+
3+
namespace ReferenceProject
4+
{
5+
public static class OptionsVerbMiddlewareExtensions
6+
{
7+
public static IApplicationBuilder UseOptionsVerbHandler(
8+
this IApplicationBuilder builder)
9+
{
10+
return builder.UseMiddleware<Middleware.OptionsVerbMiddleware>();
11+
}
12+
}
13+
}

ProjectTemplates/AspNetCore.WebApi/ReferenceProject/ReferenceProject/Startup.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414
using Newtonsoft.Json;
1515
using Newtonsoft.Json.Serialization;
1616
using ReferenceProject.Filters;
17-
using ReferenceProject.Middleware;
1817
using ReferenceProject.Modules;
1918

19+
// TODO: Create and validate configuration in web.config:
20+
// https://www.talkingdotnet.com/how-to-increase-file-upload-size-asp-net-core/
21+
// https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/?view=aspnetcore-2.1&tabs=aspnetcore2x#webconfig-file
22+
2023
namespace ReferenceProject
2124
{
2225
public class Startup
@@ -119,6 +122,9 @@ public void Configure(IApplicationBuilder app/*, IHostingEnvironment env*/)
119122
.AllowAnyHeader()
120123
.AllowCredentials());
121124

125+
// Options verb handler must be added after CORS
126+
app.UseOptionsVerbHandler();
127+
122128
app.UseMvcWithDefaultRoute();
123129

124130
Logger.LogInformation("Server started");

ProjectTemplates/AspNetCore.WebApi/ReferenceProject/ReferenceProject/web.config

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,24 @@
66
</system.web>
77

88
<system.webServer>
9+
<modules runAllManagedModulesForAllRequests="false">
10+
<remove name="WebDAVModule" />
11+
</modules>
12+
913
<handlers>
14+
<clear/>
1015
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
1116
</handlers>
17+
1218
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
19+
1320
<httpProtocol>
1421
<customHeaders>
1522
<!-- X-Powered-By - Remove the HTTP header for added security and a slight performance increase. -->
1623
<clear />
1724
</customHeaders>
1825
</httpProtocol>
26+
1927
<security>
2028
<requestFiltering>
2129
<!-- maxAllowedContentLength - Specifies the maximum length of content in a request, in bytes. The default value is 30000000 (~28.6 MB) and has

0 commit comments

Comments
 (0)