title: Content Security Policy Attribute Mvc - Adding Many Times
body: <p>I'm working with ASP.NET MVC 5. I wrote a small filter attribute for adding Content Security Policy to response header. Here is the code:</p>
  
  <pre><code>public class ContentSecurityPolicyFilterAttribute : ActionFilterAttribute
  {
      public override void OnActionExecuting(ActionExecutingContext filterContext)
      {
          HttpResponseBase response = filterContext.HttpContext.Response;
  
          response.AddHeader("Content-Security-Policy", "default-src *; " +
            "img-src * data:; " +
            "style-src 'self' 'unsafe-inline' http://fonts.googleapis.com https://fonts.googleapis.com; " +
            "script-src 'self' 'unsafe-inline' 'unsafe-eval' " +
  
            "localhost:*/* " +
  
            "https://facebook.com " + 
            "*.facebook.com " +
  
            "https://facebook.net " + 
            "*.facebook.net " +
  
            "https://onesignal.com " +
            "*.onesignal.com " +
  
            "https://abtasty.com *.abtasty.com *.convertexperiments.com " + 
  
            "http://www.googletagmanager.com " +
            "https://www.googletagmanager.com " +
  
            "http://www.google-analytics.com " +
            "https://www.google-analytics.com " +
  
            "http://www.googleadservices.com " +
            "https://www.googleadservices.com ");
  
          base.OnActionExecuting(filterContext);
      }
  }
  </code></pre>
  
  <p>To FilterConfig:</p>
  
  <pre><code>public class FilterConfig
  {
      public static void RegisterGlobalFilters(GlobalFilterCollection filters)
      {
          //...
          filters.Add(new ContentSecurityPolicyFilterAttribute());
      }
  }
  </code></pre>
  
  <p>My problem is that this code is adding more than one <code>Content-Security-Policy</code> header on every request. It should run once, and add this header one time only. Am I right?</p>
  
  <p><a href="https://i.stack.imgur.com/sZKs3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sZKs3.png" alt="CSP Header Added Too Many Times"></a></p>
  
  <p>So what can I to fix this problem ?</p>