Created
July 5, 2014 21:28
-
-
Save rodolfofadino/cbc8fcdf95015c911aa4 to your computer and use it in GitHub Desktop.
Example of WebApiConfig using WebApiThrottle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net.Http; | |
using System.Web.Http; | |
using WebApiThrottle; | |
namespace WebApplication13 | |
{ | |
public static class WebApiConfig | |
{ | |
public static void Register(HttpConfiguration config) | |
{ | |
config.MessageHandlers.Add(new ThrottlingHandler() | |
{ | |
Policy = new ThrottlePolicy(perSecond: 1, perMinute: 2) | |
{ | |
IpThrottling = true, //Configure o throttling por id | |
EndpointThrottling = true,//habilitado a diferenca por endpoint | |
ClientThrottling = true, //configura a throttling a partir do header Authorization-Token | |
//IpWhitelist = new List<string>() { } | |
//ClientWhitelist = new List<string>() { "2222"} | |
EndpointRules = new Dictionary<string, RateLimits>() | |
{ | |
{"api/values/2", new RateLimits(){PerMinute = 1}} | |
}, | |
StackBlockedRequests = true //armazena o count das requests bloqueadas | |
}, | |
Repository = new CacheRepository() | |
}); | |
// Web API configuration and services | |
// Web API routes | |
config.MapHttpAttributeRoutes(); | |
config.Routes.MapHttpRoute( | |
name: "DefaultApi", | |
routeTemplate: "api/{controller}/{id}", | |
defaults: new { id = RouteParameter.Optional } | |
); | |
} | |
} | |
} | |
//public class CustomThrottlingHandler : ThrottlingHandler | |
//{ | |
// protected override RequestIdentity SetIndentity(HttpRequestMessage request) | |
// { | |
// return new RequestIdentity() | |
// { | |
// ClientKey = request.Headers.GetValues("Rodolfo-Key").First(), | |
// ClientIp = base.GetClientIp(request).ToString(), | |
// Endpoint = request.RequestUri.AbsolutePath | |
// }; | |
// } | |
//} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment