This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathCorsProvider.cs
50 lines (46 loc) · 1.48 KB
/
CorsProvider.cs
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;
using System.Threading.Tasks;
using System.Web.Cors;
using Microsoft.Owin.Cors;
using YellowNotes.Api.Utils;
namespace YellowNotes.Api.Providers
{
internal class CorsProvider
{
private static readonly string _allowedOrgins = AppConfiguration.CorsPolicyOrigins;
private static readonly CorsPolicy _corsPolicy = new CorsPolicy { AllowAnyMethod = true, AllowAnyHeader = true };
static CorsProvider()
{
if (_allowedOrgins == "*")
{
_corsPolicy.AllowAnyOrigin = true;
}
else
{
ValidateOrgins();
}
}
public static CorsOptions GetCorsOptions()
{
return new CorsOptions
{
PolicyProvider = new CorsPolicyProvider
{
PolicyResolver = context => Task.FromResult(_corsPolicy)
}
};
}
/// <remarks>Based on System.Web.Http.Cors.EnableCorsAttribute.ValidateOrigins()</remarks>
private static void ValidateOrgins()
{
var origins = _allowedOrgins.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var uriString in origins)
{
if (!uriString.EndsWith("/") && Uri.IsWellFormedUriString(uriString, UriKind.Absolute))
{
_corsPolicy.Origins.Add(uriString);
}
}
}
}
}