1

I have Media.RequestProtection enabled in my Sitecore 10.0 instance.

Is there way to disable this only for preview and experience editor mode.

2 Answers 2

0

You can try customizing MediaProvider and GetMediaUrl to get the URL without hash while accessing media in preview mode.

  1. Create a custom MediaProvider
public class MediaProvider : Sitecore.Resources.Media.MediaProvider
    {
        public override string GetMediaUrl(MediaItem item, MediaUrlBuilderOptions options)
        {
            Assert.ArgumentNotNull(item, "item");

            bool isPreviewMode = Sitecore.Context.PageMode.IsPreview;

            var url = base.GetMediaUrl(item, options);

            if (isPreviewMode)
            {
                return url;
            }

            return HashingUtils.ProtectAssetUrl(url);
        }
    }
  1. Update the config change using the patch.
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <mediaLibrary>
      <mediaProvider patch:instead="mediaProvider[@type='Sitecore.Resources.Media.MediaProvider, Sitecore.Kernel']"
                     type="YourProjectNameSpace.MediaProvider, YourProjectNameSpace" />
  </mediaLibrary>
  </sitecore>
</configuration>

Another way is to disable for the specific core role by setting Media.RequestProtection.Enabled to false.

Patch the file to update the setting value.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <settings>
     <setting name="Media.RequestProtection.Enabled" value="false" />
    </settings>
  </sitecore>
</configuration>

Hope it helps!

5
  • How we achieve this disable for the specific core role ?
    – rahul
    Commented May 9, 2023 at 12:18
  • You can disable for CM role. Go to App_Config\Sitecore\CMS.Core\Sitecore.Media.RequestProtection.config and change as <setting name="Media.RequestProtection.Enabled" value="false" /> Commented May 9, 2023 at 12:59
  • will disabling this will have any impact in normal and experience editor mode. And is there way to disable this via patch file.
    – rahul
    Commented May 9, 2023 at 13:19
  • If you disable it will not impact any functionality for normal or experience editor mode. Yes you can do it via patch file. Commented May 9, 2023 at 13:22
  • Updated answer with patch file for updating setting value. Commented May 9, 2023 at 13:27
0

I am not sure if you can update the setting on the page request but if you see where the setting is being used in the code, I think you can control the setting. For example, I found that this is being used in the class called Sitecore.Resources.Media.MediaRequest that you can find under the DLL called Sitecore.Kernel.

I found that by extracting the whole DLL. I think using statements like Context.PageMode.IsExperienceEditorEditing or Context.PageMode.IsPreview, you can allow this setting to be true or false.

And after extending the class you can apply a patch like this to use your class.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <mediaLibrary>
      <requestParser patch:instead="*[@type='Sitecore.Resources.Media.MediaRequest, Sitecore.Kernel']" type="namespace.CustomMediaRequest,  assembly" />
    </mediaLibrary>
  </sitecore>
</configuration>

Updating my answer as per your comments:

You will find a property in the Settings.cs with public static class RequestProtection to enable of disable the Media Request Protection like below. And you can add a check here as need.

/// <summary>
/// Gets a value indicating whether Media Request Protection feature enabled.
/// </summary>
/// <value>
///   <c>true</c> if Media Request Protection feature enabled; otherwise, <c>false</c>.
/// </value>
public static bool Enabled
{
  get
  {
    if (!Settings.Media.RequestProtection._enabled.HasValue)
      Settings.Media.RequestProtection._enabled = new bool?(Settings.SettingsInstance.Media().RequestProtection().Enabled);
    return Settings.Media.RequestProtection._enabled.Value;
  }
  internal set => Settings.Media.RequestProtection._enabled = new bool?(value);
}

I hope this way you can control this.

3
  • with this approach we will be handling the behavior via code. But as this is default Sitecore OOTB functionality so this Media Request Protection should not throw error by default in preview mode also.
    – rahul
    Commented May 9, 2023 at 12:47
  • @rahul You just need to change the functionality for the code that you need and don't need to modify the rest of the code. So it will not impact the OOTB one. This way you can add the check for experience editor or preview mode. Commented May 9, 2023 at 13:51
  • Can you provide more details on code snippet. as i am seeing the values for media protection can't be modified via code.
    – rahul
    Commented May 10, 2023 at 4:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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