-
Notifications
You must be signed in to change notification settings - Fork 9.9k
/
CorsOptions.cs
98 lines (82 loc) · 3.27 KB
/
CorsOptions.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace Microsoft.AspNetCore.Cors.Infrastructure;
/// <summary>
/// Provides programmatic configuration for Cors.
/// </summary>
public class CorsOptions
{
private string _defaultPolicyName = "__DefaultCorsPolicy";
// DefaultCorsPolicyProvider returns a Task<CorsPolicy>. We'll cache the value to be returned alongside
// the actual policy instance to have a separate lookup.
internal IDictionary<string, (CorsPolicy policy, Task<CorsPolicy> policyTask)> PolicyMap { get; }
= new Dictionary<string, (CorsPolicy, Task<CorsPolicy>)>(StringComparer.Ordinal);
/// <summary>
/// Gets or sets the default policy name.
/// </summary>
public string DefaultPolicyName
{
get => _defaultPolicyName;
set
{
_defaultPolicyName = value ?? throw new ArgumentNullException(nameof(value));
}
}
/// <summary>
/// Adds a new policy and sets it as the default.
/// </summary>
/// <param name="policy">The <see cref="CorsPolicy"/> policy to be added.</param>
public void AddDefaultPolicy(CorsPolicy policy)
{
ArgumentNullException.ThrowIfNull(policy);
AddPolicy(DefaultPolicyName, policy);
}
/// <summary>
/// Adds a new policy and sets it as the default.
/// </summary>
/// <param name="configurePolicy">A delegate which can use a policy builder to build a policy.</param>
public void AddDefaultPolicy(Action<CorsPolicyBuilder> configurePolicy)
{
ArgumentNullException.ThrowIfNull(configurePolicy);
AddPolicy(DefaultPolicyName, configurePolicy);
}
/// <summary>
/// Adds a new policy.
/// </summary>
/// <param name="name">The name of the policy.</param>
/// <param name="policy">The <see cref="CorsPolicy"/> policy to be added.</param>
public void AddPolicy(string name, CorsPolicy policy)
{
ArgumentNullException.ThrowIfNull(name);
ArgumentNullException.ThrowIfNull(policy);
PolicyMap[name] = (policy, Task.FromResult(policy));
}
/// <summary>
/// Adds a new policy.
/// </summary>
/// <param name="name">The name of the policy.</param>
/// <param name="configurePolicy">A delegate which can use a policy builder to build a policy.</param>
public void AddPolicy(string name, Action<CorsPolicyBuilder> configurePolicy)
{
ArgumentNullException.ThrowIfNull(name);
ArgumentNullException.ThrowIfNull(configurePolicy);
var policyBuilder = new CorsPolicyBuilder();
configurePolicy(policyBuilder);
var policy = policyBuilder.Build();
PolicyMap[name] = (policy, Task.FromResult(policy));
}
/// <summary>
/// Gets the policy based on the <paramref name="name"/>
/// </summary>
/// <param name="name">The name of the policy to lookup.</param>
/// <returns>The <see cref="CorsPolicy"/> if the policy was added.<c>null</c> otherwise.</returns>
public CorsPolicy? GetPolicy(string name)
{
ArgumentNullException.ThrowIfNull(name);
if (PolicyMap.TryGetValue(name, out var result))
{
return result.policy;
}
return null;
}
}