In a project I'm working on we wanted to use Office Web Apps in SP2010 to preview selected documents in the browser. To do so we've created a very simple web part that renders an I-Frame with the URL set to one of the Office Web Apps urls depending on the document extension. Unfortunately the X-Frame header, that is added by the Office Web Apps service, prevents Internet Explorer to render the documents in an I-Frame! To solve this we've create a very simple HttpModule that checks for the header and changes the value from "DENY" to "SAMEORIGIN". This post simply shows the code for such a module that enables previewing of documents with Office Web Apps inside an I-Frame
/// <summary>
/// The XFrameOptionsModule loosens the x-frame policy from DENY to SAMEORIGIN
/// </summary>
public class XFrameOptionsModule : IHttpModule
{
private const string XFrameOptionsHeaderName = "X-FRAME-OPTIONS";
/// <summary>
/// Initializes a new instance of the <see cref="XFrameOptionsModule"/> class.
/// </summary>
public XFrameOptionsModule()
{
}
/// <summary>
/// Disposes of the resources (other than memory) used by the module that implements <see cref="T:System.Web.IHttpModule"/>.
/// </summary>
public void Dispose()
{
}
/// <summary>
/// Initializes a module and prepares it to handle requests.
/// </summary>
/// <param name="context">An <see cref="T:System.Web.HttpApplication"/> that provides access to the methods, properties, and events common to all application objects within an ASP.NET application</param>
public void Init(HttpApplication context)
{
context.PreSendRequestHeaders += ChangeXFrameOptionsHeaderToSameOrigin;
}
/// <summary>
/// Changes the X-Frame-Options "DENY" header to "SAMEORIGIN".
/// </summary>
/// <param name="sender">The HttpApplication that triggers the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void ChangeXFrameOptionsHeaderToSameOrigin(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpResponse response = application.Response;
string headerValue = response.Headers[XFrameOptionsHeaderName];
if (headerValue != null && headerValue.Equals("DENY", StringComparison.OrdinalIgnoreCase))
{
response.Headers[XFrameOptionsHeaderName] = "SAMEORIGIN";
}
}
}
All you have to do now is to add this module to the web.config using a SPWebConfigModification inside a feature receiver.
Cheers,
Wes
Comments have been disabled for this content.