-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOptionsMiddleware.cs
More file actions
50 lines (44 loc) · 1.69 KB
/
Copy pathOptionsMiddleware.cs
File metadata and controls
50 lines (44 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Hosting;
namespace SAPI.API
{
public class OptionsMiddleware
{
private readonly RequestDelegate _next;
private IHostingEnvironment _environment;
public OptionsMiddleware(RequestDelegate next, IHostingEnvironment environment)
{
_next = next;
_environment = environment;
}
public async Task Invoke(HttpContext context)
{
this.BeginInvoke(context);
if (context.Request.Method != "OPTIONS")
{
await this._next.Invoke(context);
}
}
private async void BeginInvoke(HttpContext context)
{
if (context.Request.Method == "OPTIONS")
{
context.Response.Headers.Add("Access-Control-Allow-Origin", new[] { (string)context.Request.Headers["Origin"] });
context.Response.Headers.Add("Access-Control-Allow-Headers", new[] { "Origin, X-Requested-With, Content-Type, Accept" });
context.Response.Headers.Add("Access-Control-Allow-Methods", new[] { "GET, POST, PUT, DELETE, OPTIONS" });
context.Response.Headers.Add("Access-Control-Allow-Credentials", new[] { "true" });
context.Response.StatusCode = 200;
await context.Response.WriteAsync("OK");
}
}
}
public static class OptionsMiddlewareExtensions
{
public static IApplicationBuilder UseOptions(this IApplicationBuilder builder)
{
return builder.UseMiddleware<OptionsMiddleware>();
}
}
}