2

I'm using Sitecore 9.0.2

What I want

I'm having an issue serving media files. I would like to check if an user is authenticated before it is served.

What I tried

I'm currently doing this for all other Sitecore Items by overriding the HttpRequestProcessor. I notice though that none of the Media Item servings pass through this pipeline.

I've also tried overriding the Preprocessrequest pipeline, but it seems that Media Items don't pass through this pipeline either?

The question

What is the best way to achieve this? I would like to be able to do something like this:

public void ProcessRequest(... request) 
{
   var item = GetMediaItem(request);
   if(!UserAuthenticated(item))
   {
    //redirect to forbidden page
   }
   //else serve media item
   base.ProcessRequest(request);
}
5

Sitecore's media files are not served via any of the pipelines, as you've already determined. Instead, look in your web.config for the following handler.

 <system.webServer>
    <handlers>
      <add verb="*" path="sitecore_media.ashx" type="Sitecore.Resources.Media.MediaRequestHandler, Sitecore.Kernel" name="Sitecore.MediaRequestHandler"/>
    </handlers>
  </system.webServer>

Override this handler, inheriting from Sitecore's own, when you want to add additional logic on top of media handling.

namespace MyProject.Custom.Media
{
    public class MediaRequestHandler : Sitecore.Resources.Media.MediaRequestHandler
    {
        protected override bool DoProcessRequest(HttpContext context, MediaRequest request,
            Sitecore.Resources.Media.Media media)
        {
            Assert.ArgumentNotNull((object) context, "context");
            Assert.ArgumentNotNull((object) request, "request");
            Assert.ArgumentNotNull((object) media, "media");

            ... YOUR CODE HERE

            return base.DoProcessRequest(context, request, media);
        }
    }
}

Full example to be found here: https://gist.github.com/jammykam/c89e2f4eb62638226e1d005d22802f0a

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.