public static ThrottlePolicyBuilder FromBuilder(ThrottlePolicyBuilder builder, string policyName)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            return(new ThrottlePolicyBuilder(policyName, builder.Requirements, builder.Exclusions));
        }
        /// <summary>
        /// Limits the bandwidth by a specified <paramref name="headerName"/>.
        /// </summary>
        /// <param name="headerName">The name of the HTTP header to look at.</param>
        /// <param name="bandwidth">The maximum bandwidth per period after which access is denied.</param>
        /// <param name="renewalPeriod">The period of time until bandwidth count is renewed.</param>
        /// <param name="sliding">Indicates wether the renewal period is sliding.</param>
        /// <returns>The current policy builder.</returns>
        public static ThrottlePolicyBuilder LimitClienBandwidthByHeader(this ThrottlePolicyBuilder builder, string headerName, long bandwidth, TimeSpan renewalPeriod, bool sliding = false)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            return(builder.AddRequirements(new HeaderApiKeyRateLimitRequirement(bandwidth, renewalPeriod, sliding, headerName)));
        }
        /// <summary>
        /// Limits the bandwidth by IP address.
        /// </summary>
        /// <param name="bandwidth">The maximum bandwidth per period after which access is denied.</param>
        /// <param name="renewalPeriod">The period of time until bandwidth count is renewed.</param>
        /// <param name="sliding">Indicates wether the renewal period is sliding.</param>
        /// <returns>The current policy builder.</returns>
        public static ThrottlePolicyBuilder LimitIPBandwidth(this ThrottlePolicyBuilder builder, long bandwidth, TimeSpan renewalPeriod, bool sliding = false)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            return(builder.AddRequirements(new IPBandwidthRequirement(bandwidth, renewalPeriod, sliding)));
        }
        /// <summary>
        /// Limits the bandwidth by a specified <paramref name="formParameter"/>.
        /// </summary>
        /// <param name="formParameter">The name of the parameter to look at.</param>
        /// <param name="bandwidth">The maximum bandwidth per period after which access is denied.</param>
        /// <param name="renewalPeriod">The period of time until bandwidth count is renewed.</param>
        /// <param name="sliding">Indicates wether the renewal period is sliding.</param>
        /// <returns>The current policy builder.</returns>
        public static ThrottlePolicyBuilder LimitClientBandwidthByFormParameter(this ThrottlePolicyBuilder builder, string formParameter, long bandwidth, TimeSpan renewalPeriod, bool sliding = false)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            return(builder.AddRequirements(new FormApiKeyBandwidthRequirement(bandwidth, renewalPeriod, sliding, formParameter)));
        }
        /// <summary>
        /// Limits the bandwidth by a specified <paramref name="routeTemplate"/>.
        /// </summary>
        /// <param name="routeTemplate">The route template. For example {apikey}/{*remaining} where "apikey" is the <paramref name="routeFragment"/>.</param>
        /// <param name="routeFragment">The name of the route fragment to look at in the route template.</param>
        /// <param name="bandwidth">The maximum bandwidth per period after which access is denied.</param>
        /// <param name="renewalPeriod">The period of time until bandwidth count is renewed.</param>
        /// <param name="sliding">Indicates wether the renewal period is sliding.</param>
        /// <returns>The current policy builder.</returns>
        public static ThrottlePolicyBuilder LimitClientBandwidthByRoute(this ThrottlePolicyBuilder builder, string routeTemplate, string routeFragment, long bandwidth, TimeSpan renewalPeriod, bool sliding = false)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            return(builder.AddRequirements(new RouteApiKeyBandwidthRequirement(bandwidth, renewalPeriod, sliding, routeTemplate, routeFragment)));
        }
예제 #6
0
        /// <summary>
        /// Adds the IP range to the IP whitelist.
        /// These addresses will pass through the policy.
        /// Valids patterns are :
        /// <list type="square">
        ///     <item>Uni address: "127.0.0.1", ":;1",</item>
        ///     <item>Uni address list: "127.0.0.1", ":;1" separated by semi-colon,</item>
        ///     <item>CIDR block: "192.168.0.0/24", "fe80::/10",</item>
        ///     <item>Begin-end : "169.258.0.0-169.258.0.255",</item>
        ///     <item>Bit mask : "192.168.0.0/255.255.255.0".</item>
        /// </list>
        /// </summary>
        /// <param name="range">
        /// The IP addresses range.
        /// </param>
        /// <returns>The current policy builder.</returns>
        public static ThrottlePolicyBuilder IgnoreIPAddressRanges(this ThrottlePolicyBuilder builder, params string[] ranges)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            return(builder.AddExclusions(new IPExclusion(ranges)));
        }
        /// <summary>
        /// Limits the calls by a specified <paramref name="queryStringParameter"/>.
        /// </summary>
        /// <param name="headerName">The name of the query string parameter to look at.</param>
        /// <param name="calls">The number of possible calls per period after which access is denied.</param>
        /// <param name="renewalPeriod">The period of time until calls count is renewed.</param>
        /// <param name="sliding">Indicates wether the renewal period is sliding.</param>
        /// <returns>The current policy builder.</returns>
        public static ThrottlePolicyBuilder LimitClientRateByQueryStringParameter(this ThrottlePolicyBuilder builder, string queryStringParameter, long calls, TimeSpan renewalPeriod, bool sliding = false)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            return(builder.AddRequirements(new QueryStringApiKeyRateLimitRequirement(calls, renewalPeriod, sliding, queryStringParameter)));
        }
        /// <summary>
        /// Limits the calls for an authenticated user.
        /// </summary>
        /// <param name="calls">The number of possible calls per period after which access is denied.</param>
        /// <param name="renewalPeriod">The period of time until calls count is renewed.</param>
        /// <param name="sliding">Indicates wether the renewal period is sliding.</param>
        /// <returns>The current policy builder.</returns>
        public static ThrottlePolicyBuilder LimitAuthenticatedUserRate(this ThrottlePolicyBuilder builder, long calls, TimeSpan renewalPeriod, bool sliding = false)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            return(builder.AddRequirements(new AuthenticatedUserRateLimitRequirement(calls, renewalPeriod, sliding)));
        }
예제 #9
0
        /// <summary>
        /// Adds a new policy.
        /// </summary>
        /// <param name="name">The name of the policy.</param>
        public ThrottlePolicyBuilder AddPolicy(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            var policyBuilder = new ThrottlePolicyBuilder(name);

            _builders.Add(policyBuilder);
            return(policyBuilder);
        }
예제 #10
0
        /// <summary>
        /// Adds the IP address to the IP whitelist.
        /// These addresses will pass through the policy.
        /// Valids patterns are :
        /// <list type="square">
        ///     <item>Uni address: "127.0.0.1", ":;1",</item>
        ///     <item>Uni address list: "127.0.0.1", ":;1" separated by semi-colon,</item>
        ///     <item>CIDR block: "192.168.0.0/24", "fe80::/10",</item>
        ///     <item>Begin-end : "169.258.0.0-169.258.0.255",</item>
        ///     <item>Bit mask : "192.168.0.0/255.255.255.0".</item>
        /// </list>
        /// </summary>
        /// <param name="address">
        /// The IP address.
        /// </param>
        /// <returns>The current policy builder.</returns>
        public static ThrottlePolicyBuilder IgnoreIPAddress(this ThrottlePolicyBuilder builder, string address)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (address == null)
            {
                throw new ArgumentNullException(nameof(address));
            }

            return(builder.IgnoreIPAddressRanges(new[] { address }));
        }