// 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.AspNetCore.Authorization { /// /// Provides programmatic configuration used by and . /// public class AuthorizationOptions { private IDictionary PolicyMap { get; } = new Dictionary(StringComparer.OrdinalIgnoreCase); /// /// The initial default policy is to require any authenticated user /// public AuthorizationPolicy DefaultPolicy { get; set; } = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build(); /// /// Add an authorization policy with the provided name. /// /// The name of the policy. /// The authorization policy. public void AddPolicy(string name, AuthorizationPolicy policy) { if (name == null) { throw new ArgumentNullException(nameof(name)); } if (policy == null) { throw new ArgumentNullException(nameof(policy)); } PolicyMap[name] = policy; } /// /// Add a policy that is built from a delegate with the provided name. /// /// The name of the policy. /// The delegate that will be used to build the 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 AuthorizationPolicyBuilder(); configurePolicy(policyBuilder); PolicyMap[name] = policyBuilder.Build(); } /// /// Returns the policy for the specified name, or null if a policy with the name does not exist. /// /// The name of the policy to return. /// The policy for the specified name, or null if a policy with the name does not exist. public AuthorizationPolicy GetPolicy(string name) { if (name == null) { throw new ArgumentNullException(nameof(name)); } return PolicyMap.ContainsKey(name) ? PolicyMap[name] : null; } } }