You can override the out-of-the-box media handler with your own like below.
Example of code of the custom Media Request Handler
using System.Web;
using Sitecore.Resources.Media;
namespace MyCustomCode.Handlers
{
public class MediaRequestHandler : Sitecore.Resources.Media.MediaRequestHandler
{
protected override bool DoProcessRequest(HttpContext context, MediaRequest request, Media media)
{
// Do your authentication and redirect logic here
return base.DoProcessRequest(context, request, media);
}
}
}
And use a patch like this.
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.webServer>
<handlers>
<add verb="*" path="sitecore_media.ashx" type="MyCustomCode.Handlers.MediaRequestHandler, MyCustomCode" name="Sitecore.MediaRequestHandler" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
</handlers>
</system.webServer>
</configuration>
Hope this helps.