views:

38

answers:

1

I created a custom RouteHandler for images that I want protected. My RouteHandler simply takes a new route (graphics/{filename}) and does a lookup for the true file path, sets the mime type, and serves it up. That works fine. (http://www.mikesdotnetting.com/Article/126/ASP.NET-MVC-Prevent-Image-Leeching-with-a-Custom-RouteHandler)

What I wanted to do next was to do a check within my custom handler for a session variable that I would set earlier to make sure the person trying to view the image had permission to do so.

Basically, they would pass a login (enter a code), which would set a session variable that I would look for in the custom RouteHandler.

My problem is that I can't seem to get at the session data from within the custom RouteHandler.

Finally, my question is: How can I set data in a controller and have it available to me from within a custom RouteHandler on a subsequent request?

+1  A: 

First, from the RequestContext passed in to the GetHttpHandler method, can you not access the Session via requestContext.HttpContext.Session? I am not sure, I could see this not working by default since it is so early in the pipeline.

If not, you can always easily move the Session checking logic into the handler by adding the IRequiresSessionState interface to your handler.

Rex M
How would I use IRequiresSessionState? My current setup is that I have a class "ImageRouteHandler" that implements IRouteHandler, then I just have a GetHttpHandler method within that serves up my image.
Brian David Berman
@Brian The ImageRouteHandler could do the session validation and return a 403 not authorized, which your application then catches and handles. Or you could set up a chained IHttpHandler where the first checks session and then executes the second if it deems it OK.
Rex M
@Rex I am still just getting null when trying to reference a session variable from within the RouteHandler
Brian David Berman
@Brian you will not get the session from the RouteHandler. Get it from the HttpHandler, as I noted in the second half of the answer.
Rex M