I have Media.RequestProtection
enabled in my Sitecore 10.0 instance.
Is there way to disable this only for preview and experience editor mode.
You can try customizing MediaProvider
and GetMediaUrl
to get the URL without hash
while accessing media in preview mode.
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);
}
}
<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!
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.