stefanprodan / WebApiThrottle

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ThrottlingFilter.cs SetIdentity(HttpRequestMessage request)

ivanhe90 opened this issue · comments

if the clientkey does not store in headers,when i override "SetIdentity" method,i can not get the request params.
I need to set the clientkey value from request params.so i suggest set the method like this "SetIdentity(HttpActionContext actionContext)". i can get more info and do more things.

By request params - do you mean querystring params? If yes, do it like so:

var apikey = new KeyValuePair<string, string>();

if (request.GetQueryNameValuePairs() != null )
{
    apiToken = request.GetQueryNameValuePairs().FirstOrDefault(q => q.Key.Equals("apikey", StringComparison.OrdinalIgnoreCase));
}

And then, you can check its value

return new RequestIdentity()
{
    ClientKey = string.IsNullOrEmpty(apikey .Value) ? "anonymous" : apikey .Value,
    ClientIp = base.GetClientIp(request).ToString(),
    Endpoint = request.RequestUri.AbsolutePath.ToLowerInvariant()
};

Another way to do it is to add these extension methods into your project so it's easier to get a hold of the querystring params.

If this is not what you meant, sorry!

In WebApi LifeCyle,when the Model Binding is complete. you can't get the request params by this method.

var apikey = new KeyValuePair<string, string>();

if (request.GetQueryNameValuePairs() != null )
{
apiToken = request.GetQueryNameValuePairs().FirstOrDefault(q => q.Key.Equals("apikey", StringComparison.OrdinalIgnoreCase));
}

Only can get params by HttpActionContext actionContext. You can get params like actionContext.ActionArguments["param1"].
@stefanprodan @mendhak