I have a web api in asp.net core 2.0. I have functionality that every request will pass the APIKey in header and my api server will read that apikey check it and allow the request if apikey is valid. I implemented it by using middleware.
Now this middleware executing before every request of whole api server. When request is coming for registration or login, API key will not there and server should allow them request without executing middleware...
- app.UseMiddleware
();
- if (context.Request.Path.Value.Contains("/user/authenticate") || context.Request.Path.Value.Contains("/user/register"))
- {
- await _next.Invoke(context); // call next middleware
- }

Tuhin PaulPosted Mar 25, 2024, 11:11 AM
Modify your
VerifyClientMiddlewareto check the request path before performing the API key validationTuhin PaulPosted Mar 25, 2024, 11:10 AM
Create a string array or list to store the paths for unrestricted access (login and registration)
Sudhesh GnanasekaranPosted Mar 24, 2024, 2:57 PM
Check out this article on how to ignore routes conditionally
https://codetrojan.com/csharp/asp-net-core-how-to-restrict-specific-routes-from-middleware/
Naimish MakwanaPosted Jan 31, 2023, 4:36 AM
Hi,
Please refer below link. It may be helpful to you.
https://www.devtrends.co.uk/blog/conditional-middleware-based-on-request-in-asp.net-core
Thanks
Naimish
adhar jainPosted Jan 31, 2023, 4:16 AM
Probably you are looking for below with some regex
app.UseWhen(context=>!context.Request.Path.Value.ToLower().Trim().Contains(@"/route"), applicationBuilder =>();
{
applicationBuilder.UseMiddleware
});
Dharmraj ThakurPosted May 27, 2018, 11:35 PM
Jignesh TrivediPosted May 27, 2018, 10:34 PM
Dharmraj ThakurPosted Mar 29, 2018, 6:03 AM
Sagar Pandurang KapPosted Mar 29, 2018, 5:11 AM