stefanprodan / WebApiThrottle

ASP.NET Web API rate limiter for IIS and Owin hosting

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Overriding ThrottlingHandler.SetIdentity is not called and produces no effect.

jonthegiant opened this issue · comments

Edit:
This was my mistake, one ThrottlingHandler was overriding the other.

Expected behavior:

  • Setting a breakpoint on ThrottlingHandler.SetIdentity should break on the return statement.

Actual behavior:

  • Execution is skipped, breakpoint is never hit.

Slight tweak to the example override:

protected override RequestIdentity SetIdentity(HttpRequestMessage request)
{
    return new RequestIdentity
                {
                    ClientKey =
                        request.Headers.Contains("Authorization")
                            ? request.Headers.GetValues("Authorization").First()
                            : "anon",
                    ClientIp = base.GetClientIp(request).ToString(),
                    Endpoint = request.RequestUri.AbsolutePath.ToLowerInvariant()
                };
}

Registration:

public static void Register(HttpConfiguration config)
{
    // Web API configuration and services

    ...

    // Add rate limiting
    config.MessageHandlers.Add(
                                new ThrottlingHandler
                                    {
                                        Policy =
                                            new ThrottlePolicy(perSecond: 1)
                                                {
                                                    ClientThrottling = true,
                                                    EndpointThrottling = true,
                                                    StackBlockedRequests = true
                                                },
                                        Repository = new CacheRepository(),
                                        QuotaExceededMessage = "Quota Exceeded Rate Limit."
                                    });

    // Register custom throttling handler.
    config.MessageHandlers.Add(new CustomThrottlingHandler());

    ...
}