stefanprodan / WebApiThrottle

ASP.NET Web API rate limiter for IIS and Owin hosting
MIT License
1.28k stars 275 forks source link

How to implement ThrottlingHandler.SetIdentity to be able to get ClientID from POST Body? #125

Open EkoGeek opened 5 years ago

EkoGeek commented 5 years ago

I want to implement ThrottlingHandler.SetIdentity so that it fetches the ClientId from a application/x-www-form-urlencoded POST like:

POST /WebAPI.DemoThrottle/Values/Throttle HTTP/1.1 Host: localhost:63621 Content-Type: application/x-www-form-urlencoded

ClientId=MyClientId&OtherParam=123

I came up with the following:

protected override RequestIdentity SetIdentity(HttpRequestMessage request) { var form = request.Content.ReadAsFormDataAsync().Result; var clientId = form["ClientId"];

But this seems to remove the body from the Action in the controller:

public async Task<IHttpActionResult> Throttle([FromBody] ThrottleData throttle)

and throttle will be null.

Any suggestions or am I doing this the wrong way?