Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ASP.NET MVC action filter for specifying an X-FRAME-OPTIONS header
/*
Copyright (C) 2013 Richard Szalay
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace RichardSzalay.Web.Security
{
public class XframeOptionsAttribute : ActionFilterAttribute
{
XframeOption option;
string allowFrom;
public string AllowFrom
{
get { return allowFrom; }
set { allowFrom = value; option = XrameOption.AllowFrom; }
}
public XframeOption Option
{
get { return option; }
set { option = value; }
}
public XframeOptionsAttribute()
: this(XframeOption.Deny)
{
}
public XframeOptionsAttribute(XframeOption option)
{
this.Option = option;
}
public override void OnResultExecuting(System.Web.Mvc.ResultExecutingContext filterContext)
{
filterContext.HttpContext.Response.AddHeader("x-frame-options", XframeOption(option, allowFrom));
}
static string FormatOption(XframeOption option, string allowFrom)
{
if (option == XframeOption.AllowFrom)
{
return "ALLOW-FROM " + allowFrom;
}
return option.ToString("G").ToUpperInvariant();
}
}
public enum XframeOption
{
Deny = 0,
SameOrigin = 1,
AllowFrom = 2
}
}
@dcastroamg
Copy link

dcastroamg commented Sep 13, 2016

you do know your code doesn't compile right?

@Ahoapap
Copy link

Ahoapap commented Mar 15, 2017

`
public class XframeOptionsAttribute : ActionFilterAttribute
{
XframeOption option;
string allowFrom;

    public string AllowFrom
    {
        get { return allowFrom; }
        set { allowFrom = value; option = XframeOption.AllowFrom; }
    }

    public XframeOption Option
    {
        get { return option; }
        set { option = value; }
    }

    public XframeOptionsAttribute()
      : this(XframeOption.Deny)
    {

    }

    public XframeOptionsAttribute(XframeOption option)
    {
        this.Option = option;
    }

    public override void OnResultExecuting(System.Web.Mvc.ResultExecutingContext filterContext)
    {
        filterContext.HttpContext.Response.AddHeader("x-frame-options", FormatOption(option, allowFrom));
    }

    static string FormatOption(XframeOption option, string allowFrom)
    {
        if (option == XframeOption.AllowFrom)
        {
            return "ALLOW-FROM " + allowFrom;
        }

        return option.ToString("G").ToUpperInvariant();
    }
}

public enum XframeOption
{
    Deny = 0,
    SameOrigin = 1,
    AllowFrom = 2
}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment