// Copyright (c) Microsoft Open Technologies, Inc. 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;
using Microsoft.AspNet.Cors.Core;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Cors.Core
{
///
/// 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("value");
}
_defaultPolicyName = value;
}
}
///
/// Adds a new policy.
///
/// The name of the policy.
/// The policy to be added.
public void AddPolicy([NotNull] string name, [NotNull] CorsPolicy 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([NotNull] string name, [NotNull] Action 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([NotNull] string name)
{
return PolicyMap.ContainsKey(name) ? PolicyMap[name] : null;
}
}
}