Git Product home page Git Product logo

Comments (5)

muxa avatar muxa commented on September 21, 2024 12

I have the same question. As a workaround i've used "ClientIdHeader": "Authorization", however the API can be configured to accept JWT tokens via query strings.

The problem is that the client rate limiting middleware is executed before MVC authorisation handles parse JWT token, and so httpContext.User is not set.

It would be nice to have an option for ClientRateLimitMiddleware to be executed after user context is set.

from aspnetcoreratelimit.

Kantis avatar Kantis commented on September 21, 2024 1

I had a similar problem with wanting to use claims-based ratelimiting. My solution was to register the ratelimiting middleware after authentication. In Startup.cs#Configure:

			app.UseAuthentication();
			app.UseClientRateLimiting();

Then using a custom configuration and resolver:

public class ClientIdResolver : IClientResolveContributor
	{
		private readonly IHttpContextAccessor _httpContextAccessor;

		public ClientIdResolver(IHttpContextAccessor httpContextAccessor)
		{
			_httpContextAccessor = httpContextAccessor;
		}
		
		public string ResolveClient()
		{
			return _httpContextAccessor.HttpContext.User.GetPlayerId().ToString();
		}
	}
	{
		public CustomRateLimitConfiguration(IHttpContextAccessor httpContextAccessor,
											IOptions<IpRateLimitOptions> ipOptions,
											IOptions<ClientRateLimitOptions> clientOptions) : base(
			httpContextAccessor, ipOptions, clientOptions)
		{
		}
		
		protected override void RegisterResolvers()
		{
			ClientResolvers.Add(new ClientIdResolver(HttpContextAccessor));
		}
	}```

register the custom configuration using services in `Startup.cs#ConfigureServices`:
`services.AddSingleton<IRateLimitConfiguration, CustomRateLimitConfiguration>();`

from aspnetcoreratelimit.

CurlyBraceTT avatar CurlyBraceTT commented on September 21, 2024

@Kantis
What do you use as Authorization? Custom middleware? I have custom AuthenticationHandler and a custom AuthorizationHandler and they execute before IClientResolveContributor. Maybe because they are MVC filters, which required to be executed after rate limiter.

Seems like I need to write middleware for Authentication then and use actual handlers just to confirm user.

from aspnetcoreratelimit.

Kantis avatar Kantis commented on September 21, 2024

@Kantis
What do you use as Authorization? Custom middleware? I have custom AuthenticationHandler and a custom AuthorizationHandler and they execute before IClientResolveContributor. Maybe because they are MVC filters, which required to be executed after rate limiter.

Seems like I need to write middleware for Authentication then and use actual handlers just to confirm user.

We used claim based authentication using an auth header. We tried to use as much out-of-the-box as possible, so it shouldn't have been any custom middleware, but I'm not active in the project anymore so can't say for sure.

from aspnetcoreratelimit.

awatertrevi avatar awatertrevi commented on September 21, 2024

This helped me: #82 (comment)

from aspnetcoreratelimit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.