Authorization middleware #96
-
Hi, I need to make sure that certain routes are blocked to anonymous users. Thanks a lot for this nice project ! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
So, you have a route that generates the token and sends it back to the client. Now you need some middleware that validates that the token gets sent back and is valid on other routes. Do I understand your question correctly? |
Beta Was this translation helpful? Give feedback.
-
There are two places you can hook into to make this check. You can do it either when the request is received by the server, or prior to the request being routed. Handlers for both of these hooks are executed asynchronously.
While you can add this hook at any time, I'd recommend doing it in your startup file where you configure the server: public void ConfigureServer(IRestServer server)
{
// You can do this as a lambda
server.Router.BeforeRoutingAsync += async (ctx) =>
{
// your code here
};
// Or you can reference another method somewhere (static is easiest)
server.Router.BeforeRoutingAsync += MyJwtClass.MethodToValidateJwt;
} |
Beta Was this translation helpful? Give feedback.
There are two places you can hook into to make this check. You can do it either when the request is received by the server, or prior to the request being routed. Handlers for both of these hooks are executed asynchronously.
server.OnRequestAsync
handlers are delegates of typeRequestReceivedAsyncEventHandler
, and run when the request has been received by the server. This is useful when you want code to run independent of whether the request is routed. For example, requests for static files served from content folders will not be routed.router.BeforeRoutingAsync
handlers are delegates of typeRoutingAsyncEventHandler
, and run just before any routes are executed (and there is a corresp…