// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic; namespace Microsoft.AspNet.Cors.Infrastructure { /// /// Provides programmatic configuration for Cors. /// public class CorsOptions { private string _defaultPolicyName = "__DefaultCorsPolicy"; private IDictionary PolicyMap { get; } = new Dictionary(); public string DefaultPolicyName { get { return _defaultPolicyName; } set { if (value == null) { throw new ArgumentNullException(nameof(value)); } _defaultPolicyName = value; } } /// /// Adds a new policy. /// /// The name of the policy. /// The policy to be added. public void AddPolicy(string name, CorsPolicy policy) { if (name == null) { throw new ArgumentNullException(nameof(name)); } if (policy == null) { throw new ArgumentNullException(nameof(policy)); } PolicyMap[name] = policy; } /// /// Adds a new policy. /// /// The name of the policy. /// A delegate which can use a policy builder to build a policy. public void AddPolicy(string name, Action configurePolicy) { if (name == null) { throw new ArgumentNullException(nameof(name)); } if (configurePolicy == null) { throw new ArgumentNullException(nameof(configurePolicy)); } var policyBuilder = new CorsPolicyBuilder(); configurePolicy(policyBuilder); PolicyMap[name] = policyBuilder.Build(); } /// /// Gets the policy based on the /// /// The name of the policy to lookup. /// The if the policy was added.null otherwise. public CorsPolicy GetPolicy(string name) { if (name == null) { throw new ArgumentNullException(nameof(name)); } return PolicyMap.ContainsKey(name) ? PolicyMap[name] : null; } } }