ApiKey from route parameter and not querystring.
frederic-thibault opened this issue · 4 comments
Hi,
This is not an issue but more a question or request.
The requirement I have is to be able to register a call back url to my system to receive notification. I want to add the apikey in the url since I don't have the options to request something in a header or has a querystring parameter. It needs to be a simple url
http://1.2.3.4/{apikey}/somethingelse
[Route["{apikey}/somethingelse"]
public ...
There is a way to validate the api FromRoute parameter?
Thanks,
I am afraid this middleware does not support this.
If you are not in control of changing the client/source from where the request is originated then may be you can try using UrlRewrite to read the apikey from URL route and append it as a query parameter if it works?
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/url-rewriting?view=aspnetcore-8.0
Thanks, I have fork the repo add ApiKeyInRouteValuesHandler. I just have to do this in the code.
`protected override Task ParseApiKeyAsync()
{
#if NET6_0_OR_GREATER
if (Request.RouteValues.ContainsKey(Options.KeyName) && Request.RouteValues[Options.KeyName] != null)
{
var value = Request.RouteValues[Options.KeyName].ToString();
return Task.FromResult(value);
}
#endif
// No ApiKey route value found
return Task.FromResult(string.Empty);
}`
After adding unit tests and fix compatibility issues, I can submit a PR if you want.
Thanks for this suggestion @frederic-thibault . I have added this ability to the library with the latest 8.0.1 release.
Thanks very much @mihirdilip , it works like a charm.