Dharmraj Thakur

Dharmraj Thakur

  • 283
  • 7.6k
  • 360.9k

Middleware should ignore specific routes in .net core

Mar 29 2018 2:58 AM
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...
  1. app.UseMiddleware<VerifyClientMiddleware>();  
 I did temporary solution which I don't want... I wrote a condition in middleware which allow login and registration api.
  1. if (context.Request.Path.Value.Contains("/user/authenticate") || context.Request.Path.Value.Contains("/user/register"))  
  2. {  
  3.     await _next.Invoke(context); // call next middleware  
  4. }  
 Thanks in advance.

Brought to you by:

Answers (4)