Using CSP Header in ASP.NET Core 2.0


How to use Content-Security-Policy header in ASP.NET Core to prevent XSS attacks.


Create an empty project and update Startup to include service and middleware for MVC:

Add a _Layout page and include scripts, styles and images from local (wwwroot) and remote domains:

Run and observe the errors when loading the page, browser has rejected loading from remote sources:


Content Security Policy (CSP) is an additional level of security that could help prevent Cross Site Scripting (XSS) attacks. In these attacks malicious scripts are executed on user’s browser since browser doesn’t know whether the source of the script is trustworthy or not.

CSP allow developers to specify the sources (domains) that trustworthy and can serve executable scripts. This whitelisting of domains is achieved by using Content-Security-Type HTTP header like:

Content-Security-Policy: [policy]

Here [policy] is made up of directives describing the type of restrictions and domains to whitelist. It is a string defining a series of directives and sources separated by semicolon:

[directive] <source> <…source>; [directive] <source> <…source>; …


Sources are the trusted domains and commonly used ones are:

  • *: allow any URL.
  • ‘self’: allow the origin of served page. Note that single quotes are required.
  • ‘none’: no source should be allowed. Note that single quotes are required.
  • Host: allow a specified internet host (by name or IP address). A wildcard (asterisk character) can be used to include all subdomains e.g. http://*
  • ‘unsafe-line’: allow inline scripts
  • ‘nonce-[base64-value]’: allow inline scripts with a specific nonce (number used once). The nonce should be encrypted and unique for every HTTP request/response.

Note: other sources could be found at Mozilla documents for developers, see Other Resources section at the end of this post.


Directives specify the resource types to put restrictions on. Commonly used ones are:

  • script-src: defines valid sources of JavaScript
  • style-src: defines valid sources of stylesheets
  • img-src: defines valid sources of images
  • connect-src: defines valid sources to which AJAX calls can be made
  • font-src: defines valid sources of fonts
  • object-src: defines valid sources for <object>, <embed> and <applet> elements
  • media-src: defines valid sources of audio and video
  • form-action: defines valid sources that can be used as a HTML <form> action.
  • default-src: specifies the default policy for loading content

Note: other directives could be found at Mozilla documents for developers, see Other Resources section at the end of this post.


We could encapsulate the building and adding of CSP header in a reusable middleware. Below is an example to get you started, you could extend as necessary. First create a class to hold sources:

Create a builder we’ll use when setting up the middleware:

Now we can create a middleware:

And an extension method to set it up:

We can now configure the middleware in Startup class:

Run and observe the network traffic, browser is allowing local and remote resources:

Other Resources

I’ve only scratched the surface of security headers and CSP. These are some of the other useful resources you could look at:

  • Pluralsight course by Troy Hunt discussing security headers
  • Mozilla developer documentation on HTTP headers.
  • Joonas blog and sample for various security headers.

Source Code


Leave a Reply