Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ApiKey from route parameter and not querystring. #41

Closed
frederic-thibault opened this issue Jan 26, 2024 · 4 comments
Closed

ApiKey from route parameter and not querystring. #41

frederic-thibault opened this issue Jan 26, 2024 · 4 comments

Comments

@frederic-thibault
Copy link

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,

@mihirdilip
Copy link
Owner

Hi @frederic-thibault

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

@frederic-thibault
Copy link
Author

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.

@mihirdilip
Copy link
Owner

Thanks for this suggestion @frederic-thibault . I have added this ability to the library with the latest 8.0.1 release.

@frederic-thibault
Copy link
Author

frederic-thibault commented Jan 30, 2024

Thanks very much @mihirdilip , it works like a charm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants