Last active
January 29, 2018 15:52
-
-
Save hombreDelPez/eee0591b5dfa053243f3c91d0e979e7d to your computer and use it in GitHub Desktop.
Setting Response Headers - HttpRequestProcessor - Context Item
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Sitecore; | |
using Sitecore.Data; | |
using Sitecore.Pipelines.HttpRequest; | |
namespace YourProject.Core.Processors | |
{ | |
public class ResponseHeaders : HttpRequestProcessor | |
{ | |
private readonly ID _responseHeadersItem = new ID("{2C9586BD-4384-4DBF-B595-CF914572B9CD}"); | |
private const string CspFieldName = "Content Security Policy"; | |
private const string XFrameOptionsFieldName = "X Frame Options"; | |
public override void Process(HttpRequestArgs args) | |
{ | |
if (Context.Item == null || Context.Database == null || | |
!Context.Site.Name.Equals("yourSiteName", StringComparison.InvariantCultureIgnoreCase)) | |
{ | |
return; | |
} | |
var responseHeadersItem = Context.Database.GetItem(_responseHeadersItem); | |
if (responseHeadersItem == null) | |
{ | |
return; | |
} | |
var cspValue = string.IsNullOrEmpty(Context.Item[CspFieldName]) | |
? responseHeadersItem[CspFieldName] | |
: Context.Item[CspFieldName]; | |
if (!string.IsNullOrEmpty(cspValue)) | |
{ | |
args.Context.Response.Headers["Content-Security-Policy"] = cspValue; | |
} | |
var xFrameOptionsValue = string.IsNullOrEmpty(Context.Item[XFrameOptionsFieldName]) | |
? responseHeadersItem[XFrameOptionsFieldName] | |
: Context.Item[XFrameOptionsFieldName]; | |
if (!string.IsNullOrEmpty(xFrameOptionsValue)) | |
{ | |
args.Context.Response.Headers["X-Frame-Options"] = xFrameOptionsValue; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment