Security Headers for ASP.Net and .Net CORE
Tags: securityheaders, dotnet, infosec, appsec
Published: 2019-03-23 23:51:08

---
title: Security Headers for ASP.Net and .Net CORE
published: true
description: Tanya Janca dissects how to applying security headers differs for ASP.Net versus .Net CORE. 
tags: securityheaders, DotNet, infosec, appsec
---

For those who do not follow [myself](https://twitter.com/shehackspurple) or [Franziska Bühler](https://twitter.com/bufrasch), we have an open source project together called [OWASP DevSlop](https://devslop.co) in which we explore DevSecOps through writing vulnerable apps, creating pipelines, publishing proof of concepts, and documenting what we’ve learned on our [YouTube Channel](https://aka/ms/DevSlopShow) and our blogs. In this article we will explore adding security headers to our proof of concept website, DevSlop.co. This blog post is closely related to Franziska’s post [OWASP DevSlop’s journey to TLS and Security Headers](https://medium.com/@franbuehler/owasp-devslops-journey-to-tls-and-security-headers-aa892f1ac851). If you like this one, read hers too. :)

Franziska Bühler and I installed several security headers during the OWASP DevSlop Show in Episode [2](https://www.youtube.com/watch?v=-il28nDFgBg&t=3s), [2.1](https://www.youtube.com/watch?v=6-mqK3jr6Jg&t=3s&list=PLI9RITMnVbyg-OEn2vbsDnzWi9jGd6_5u&index=5) and [2.2](https://www.youtube.com/watch?v=uJeUodTRCEs). Unfortunately we found out that .Net Core apps don’t have a web.config, so the next time we published it wiped out the beautiful security headers we had added. Although that is not good news, it was another chance to learn, and it gave me great excuse to finally write my **Security Headers** blog post that I have been promising. Here we go!


![Our web.config looked so…. Empty.](https://thepracticaldev.s3.amazonaws.com/i/hy9wb6dmqphd0xz4ys6o.png)

Just now, I added back the headers but I added them to the startup.cs file in my .Net Core app, which you can [watch here](https://www.youtube.com/watch?v=uJeUodTRCEs). Special thanks to [Damien Bod](https://damienbod.com/2018/02/08/adding-http-headers-to-improve-security-in-an-asp-net-mvc-core-application/) for help with the [.Net Core twist](https://docs.microsoft.com/en-ca/dotnet/core/?WT.mc_id=SheHacksPurple-Blog-tajanca).

If you want in-depth details about what we did on the show and what each security header means, you should [read Franziska’s blog post](https://medium.com/@franbuehler/owasp-devslops-journey-to-tls-and-security-headers-aa892f1ac851). She explains every step, and if you are trying to add security headers for the first time to your web.config ([ASP.Net](https://docs.microsoft.com/en-ca/dotnet/standard/?WT.mc_id=SheHacksPurple-Blog-tajanca), not [.Net CORE](https://docs.microsoft.com/en-ca/dotnet/core/?WT.mc_id=SheHacksPurple-Blog-tajanca)), you should definitely read it.

The new code for ASP.Net in your web.config looks like this:
```
**<! — Start Security Headers ->**
<httpProtocol>
 <customHeaders>
 <add name=”X-XSS-Protection” value=”1; mode=block”/>
 <add name=”Content-Security-Policy” value=”default-src ‘self’”/>
 <add name=”X-frame-options” value=”SAMEORIGIN”/>
 <add name=”X-Content-Type-Options” value=”nosniff”/>
 <add name=”Referrer-Policy” value=”strict-origin-when-cross-origin”/>
 <remove name=”X-Powered-By”/>
 </customHeaders>
 </httpProtocol>
**<! — End Security Headers ->**
```

![Our new-and-improved Web.Config!](https://thepracticaldev.s3.amazonaws.com/i/ofvzgoxzoqyxclab4oqi.png)


And the new code for my startup.cs (.Net CORE), looks like this (Thank you Damien Bod):

**//Security headers make me happy**
app.UseHsts(hsts => hsts.MaxAge(365).IncludeSubdomains());
app.UseXContentTypeOptions();
app.UseReferrerPolicy(opts => opts.NoReferrer());
app.UseXXssProtection(options => options.EnabledWithBlockMode());
app.UseXfo(options => options.Deny());
app.UseCsp(opts => opts
.BlockAllMixedContent()
.StyleSources(s => s.Self())
.StyleSources(s => s.UnsafeInline())
.FontSources(s => s.Self())
.FormActions(s => s.Self())
.FrameAncestors(s => s.Self())
.ImageSources(s => s.Self())
.ScriptSources(s => s.Self())
);
 **//End Security Headers**

![Behold, our beautiful security headers!](https://thepracticaldev.s3.amazonaws.com/i/zsbe9pmu9bb2iwqioskd.png)

In future episodes we will also add:
* Secure settings for our cookies
* X-Permitted-Cross-Domain-Policies: none
* Expect-CT: (not currently supported by our provider)
* Feature-Policy: camera ‘none’; microphone ‘none’; speaker ‘self’; vibrate ‘none’; geolocation ‘none’; accelerometer ‘none’; ambient-light-sensor ‘none’; autoplay ‘none’; encrypted-media ‘none’; gyroscope ‘none’; magnetometer ‘none’; midi ‘none’; payment ‘none’; picture-in-picture ‘none’; usb ‘none’; vr ‘none’; fullscreen *;

For more information on all of these security headers, I strongly suggest you read the [OWASP Security Headers Guidance](https://www.owasp.org/index.php/OWASP_Secure_Headers_Project#xpcdp).

We now have good marks from all of the important places, [https://securityheaders.com](https://securityheaders.com), [https://www.ssllabs.com](https://www.ssllabs.com) and [http://hardenize.com)](http://hardenize.com), but hope to improve our score even further.

For more information, watch our show on [YouTube](https://aka.ms/DevSlopShow)!
![SSLLabs for the win!](https://thepracticaldev.s3.amazonaws.com/i/3g3boahgnpvon4qlj916.png)


#####Please use every security header that is available and applicable to you.