<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Secured Programming - TheCodeBuzz</title>
	<atom:link href="https://thecodebuzz.com/category/secured-programming/feed/" rel="self" type="application/rss+xml" />
	<link>https://thecodebuzz.com</link>
	<description>Best Practices for Software Development</description>
	<lastBuildDate>Sun, 03 Mar 2024 01:43:11 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://thecodebuzz.com/wp-content/uploads/2022/11/cropped-android-chrome-512x512-1-1-51x51.jpg</url>
	<title>Secured Programming - TheCodeBuzz</title>
	<link>https://thecodebuzz.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Resolved-Failed to determine the HTTPS port for the redirect</title>
		<link>https://thecodebuzz.com/failed-to-determine-the-https-port-for-the-redirect/</link>
					<comments>https://thecodebuzz.com/failed-to-determine-the-https-port-for-the-redirect/#comments</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Mon, 05 Sep 2022 03:38:00 +0000</pubDate>
				<category><![CDATA[Secured Programming]]></category>
		<category><![CDATA[failed to determine the https port for redirect aws]]></category>
		<category><![CDATA[failed to determine the https port for redirect azure app]]></category>
		<category><![CDATA[failed to determine the https port for redirect kubernetes]]></category>
		<category><![CDATA[failed to determine the https port for redirect net 6]]></category>
		<category><![CDATA[failed to determine the https port for redirect nginx]]></category>
		<category><![CDATA[failed to determine the https port for redirect pcf]]></category>
		<category><![CDATA[failed to determine the https port for redirect. .net 5]]></category>
		<category><![CDATA[failed to determine the https port for redirect. docker]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=14642</guid>

					<description><![CDATA[<p>In this article, we shall see a few guidelines to resolve the error &#8220;Failed to determine the HTTPS port for the redirect&#8221; while configuring the HTTP Strict Transport Security (HSTS)&#160;header in your website or API. Today in this article, we will cover below aspects, Issue Description ASP.NET Core API or Website runtime gives below error, [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/failed-to-determine-the-https-port-for-the-redirect/">Resolved-Failed to determine the HTTPS port for the redirect</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>In this article, we shall see a few guidelines to resolve the error &#8220;Failed to determine the HTTPS port for the redirect&#8221; while configuring the HTTP <strong><em>Strict Transport Security (HSTS)&nbsp;</em></strong>header in your website or API.</p>



<p></p>



<p>Today in this article, we will cover below aspects,</p>



<p></p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#block-479e0224-2ebd-448b-8c18-6eb8e07c10b2">Issue Description</a></li><li><a href="#block-fc231335-5fd9-4694-b86b-9600088cdc6c">Resolution</a></li><li><a href="#aioseo-approach-1">Approach 1  &#8211; Configure X-Forwarded headers</a></li><li><a href="#aioseo-approach-2">Approach 2- Setting up HTTP Port</a></li></ul></div>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="block-479e0224-2ebd-448b-8c18-6eb8e07c10b2">Issue Description</h2>



<p></p>



<p>ASP.NET Core API or Website runtime gives below error,</p>



<p></p>



<pre class="wp-block-preformatted has-cyan-bluish-gray-background-color has-background"><span style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-red-color">Failed to determine the HTTPS port for the redirect</span></pre>



<p></p>



<h2 class="wp-block-heading" id="block-fc231335-5fd9-4694-b86b-9600088cdc6c">Resolution</h2>



<p></p>



<p>This issue generally occurs when configuring the <strong><em>HTTP Strict Transport Security (<a href="https://www.thecodebuzz.com/http-strict-transport-security-hsts-security-headers/" target="_blank" rel="noopener" title="HTTPS Vs HTTP- Top 10 Benefits and Differences">HSTS</a>)&nbsp;</em></strong>header in the website or API. It is recommended to use <a href="https://www.thecodebuzz.com/https-vs-http-top-10-benefits-differences-ssl-tls-seo/" target="_blank" rel="noopener" title="HTTPS Vs HTTP- Top 10 Benefits and Differences">HTTPS Vs HTTP</a> protocol.</p>



<p></p>



<p>This is the default ASP.NET behavior where the recommendation is as below,</p>



<p></p>



<ul class="wp-block-list">
<li>The use of HSTS means <strong>all requests will be routed to HTTPS.</strong> </li>



<li>Also, the ability to<em><strong> re-direct insecure requests(HTTP) to secure HTTPS</strong></em>. </li>
</ul>



<p></p>



<p>Below is the sample code,</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; title: ; notranslate">
 
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

            app.UseHttpsRedirection();

.
.

     }
   
</pre></div>


<p></p>



<p>Above we have used middleware <strong><em>UseHttpsRedirection </em></strong>which redirects any<strong><em> HTTP request </em></strong>to a<em><strong> secured HTTPS </strong></em>request.</p>



<p></p>



<p>While redirecting the port must be available to redirect the request. </p>



<p></p>



<p>This can also be achieved by setting the variable <em><strong>port using any of the approaches discussed.</strong></em> </p>



<p></p>



<p></p>



<p>All the below approaches ultimately set the environment variable <strong><em>ASPNETCORE_HTTPS_PORT</em></strong>. Please see below for more details on various approaches to setting up the port.</p>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-approach-1">Approach 1  &#8211; Configure <strong><em><code>X-Forwarded</code></em></strong> headers</h2>



<p></p>



<p>Configure the API middleware with&nbsp;ForwardedHeadersOptions&nbsp;to forward the two headers in the headers in&nbsp;<strong><em><code>Startup.ConfigureServices</code>.</em></strong></p>



<p></p>



<ul class="has-medium-font-size wp-block-list">
<li><strong><em><code>X-Forwarded-For</code>&nbsp;</em></strong></li>
</ul>



<ul class="has-medium-font-size wp-block-list">
<li><strong><em><code>X-Forwarded-Proto</code>&nbsp;</em></strong></li>
</ul>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; highlight: [5,6,7,8,9]; title: ; notranslate">
  public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();

            services.Configure&amp;lt;ForwardedHeadersOptions&gt;(options =&gt;
            {
                options.ForwardedHeaders =
                    ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
            });
       ...
       ...

        }
</pre></div>


<p></p>



<p></p>



<p>Also, update the configure method as below,</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; highlight: [3]; title: ; notranslate">
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseForwardedHeaders();

        if (env.IsDevelopment())
        {
             app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseHsts();
        }
</pre></div>


<p></p>



<p></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>Please note to enable this middleware as the <em><strong>first </strong></em>middleware in the API Pipeline order.</p>
</blockquote>



<p></p>



<p>If you see any issues with the above approach, please try setting up an HTTP port explicitly to resolve the issue which can be achieved using any of the below approaches.</p>



<p></p>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-approach-2">Approach 2- Setting up HTTP Port</h2>



<p></p>



<p>If the above approach for the Proto header forward doesn&#8217;t work for you, you may try the below approach.</p>



<p></p>



<p></p>



<p>All the below approaches to set the environment variable <strong><em>ASPNETCORE_HTTPS_PORT</em></strong>, you can use any of the below approaches to do the same.</p>



<p></p>



<ul class="wp-block-list">
<li>Set the environment variable <strong><em>ASPNETCORE_HTTPS_PORT</em></strong> explicitly with the port number.</li>
</ul>



<p></p>



<ul class="wp-block-list">
<li>Using<em><strong> apsettings.json </strong></em>, please add the key value as &#8220;<strong><em>https_port</em></strong>&#8220;: 443</li>
</ul>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; highlight: [2]; title: ; notranslate">
{
    &quot;https_port&quot;: 443,

    &quot;Logging&quot;: {
        &quot;LogLevel&quot;: {
            &quot;Default&quot;: &quot;Information&quot;,
            &quot;Microsoft&quot;: &quot;Warning&quot;,
            &quot;Microsoft.Hosting.Lifetime&quot;: &quot;Information&quot;
        }
    },
    &quot;AllowedHosts&quot;: &quot;*&quot;
}
</pre></div>


<p></p>



<ul class="wp-block-list">
<li>HTTPS port can also be set by using the <em><strong>AddHttpsRedirection&nbsp;</strong></em>middleware option as below,</li>
</ul>



<p></p>



<figure class="wp-block-image size-large is-resized"><img fetchpriority="high" decoding="async" width="1024" height="769" src="https://www.thecodebuzz.com/wp-content/uploads/2021/01/Failed-to-determine-the-HTTPS-port-1024x769.jpg" alt="Failed to determine the HTTPS port for the redirect" class="wp-image-14653" srcset="https://thecodebuzz.com/wp-content/uploads/2021/01/Failed-to-determine-the-HTTPS-port-1024x769.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/01/Failed-to-determine-the-HTTPS-port-300x225.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/01/Failed-to-determine-the-HTTPS-port-768x577.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/01/Failed-to-determine-the-HTTPS-port-692x520.jpg 692w, https://thecodebuzz.com/wp-content/uploads/2021/01/Failed-to-determine-the-HTTPS-port.jpg 1379w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<ul class="wp-block-list">
<li>To set the port number for <strong><em>https_port</em></strong> use configuration or call&nbsp;<strong><em><code>UseSetting</code> </em></strong>by using <a href="https://www.thecodebuzz.com/createdefaultbuilder-configuration-management-net-core-and-asp-net-core/" target="_blank" rel="noreferrer noopener" title="CreateDefaultBuilder and Configuration Management in .NET/ASP.NET Core"><strong><em>Generic Host Builder</em></strong></a></li>
</ul>



<p></p>



<figure class="wp-block-image size-large is-resized"><img decoding="async" width="1024" height="309" src="https://www.thecodebuzz.com/wp-content/uploads/2021/01/Failed-to-determine-the-HTTPS-port-using-generic-host-builder-1024x309.jpg" alt="HSTS Failed to determine the HTTPS port for the redirect" class="wp-image-14654" srcset="https://thecodebuzz.com/wp-content/uploads/2021/01/Failed-to-determine-the-HTTPS-port-using-generic-host-builder-1024x309.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/01/Failed-to-determine-the-HTTPS-port-using-generic-host-builder-300x90.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/01/Failed-to-determine-the-HTTPS-port-using-generic-host-builder-768x231.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/01/Failed-to-determine-the-HTTPS-port-using-generic-host-builder-1536x463.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2021/01/Failed-to-determine-the-HTTPS-port-using-generic-host-builder-785x237.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2021/01/Failed-to-determine-the-HTTPS-port-using-generic-host-builder.jpg 1828w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>If the reverse proxy already handles HTTPS redirection, then <strong><em>don&#8217;t use HTTPS Redirection Middleware.&nbsp;</em></strong></p>
</blockquote>



<p></p>



<p><strong><em>References :</em></strong></p>



<p></p>



<ul class="wp-block-list">
<li><a href="https://www.thecodebuzz.com/http-strict-transport-security-hsts-security-headers/" target="_blank" rel="noreferrer noopener"><strong><em>ASP.NET Core adds HSTS Security Headers</em></strong></a></li>
</ul>



<p></p>



<p id="block-cee69911-1a09-4f21-8ae5-fbfc75de9c8d">Did I miss anything else in these resolution steps?</p>



<p id="block-36fa2d09-9929-4fce-8a40-9e86a4ec3115">Did the above steps resolve your issue? Please sound off your comments below!</p>



<p></p>



<p id="block-d725879f-6ff3-4969-8cd1-9a890eff9385">Happy Coding !!</p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p><p>The post <a href="https://thecodebuzz.com/failed-to-determine-the-https-port-for-the-redirect/">Resolved-Failed to determine the HTTPS port for the redirect</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/failed-to-determine-the-https-port-for-the-redirect/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
		<item>
		<title>ASP.NET Core How to Add HSTS Security Headers</title>
		<link>https://thecodebuzz.com/http-strict-transport-security-hsts-security-headers/</link>
					<comments>https://thecodebuzz.com/http-strict-transport-security-hsts-security-headers/#comments</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Thu, 05 May 2022 02:49:00 +0000</pubDate>
				<category><![CDATA[Secured Programming]]></category>
		<category><![CDATA[asp.net core ssl certificate]]></category>
		<category><![CDATA[asp.net core ssl certificate localhost]]></category>
		<category><![CDATA[disable https in asp.net core 6]]></category>
		<category><![CDATA[err_http2_inadequate_transport_security asp net core]]></category>
		<category><![CDATA[how to enable https in asp.net core web api]]></category>
		<category><![CDATA[hsts asp.net mvc]]></category>
		<category><![CDATA[strict-transport-security header]]></category>
		<category><![CDATA[usehttpsredirection]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=14535</guid>

					<description><![CDATA[<p>Today in this article, we shall see in ASP.NET Core how to Add HSTS Security Headers to Web/API applications. Today we will cover the following aspects in this article, Most of these headers once added to response headers use in-built browser features to protect your data and communication over the network. Types of Security Headers [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/http-strict-transport-security-hsts-security-headers/">ASP.NET Core How to Add HSTS Security Headers</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="257" src="https://www.thecodebuzz.com/wp-content/uploads/2022/05/Strict-Transport-Security-1024x257.jpg" alt="ASP.NET Core How to Add HSTS Security Headers" class="wp-image-22380" srcset="https://thecodebuzz.com/wp-content/uploads/2022/05/Strict-Transport-Security-1024x257.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2022/05/Strict-Transport-Security-300x75.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2022/05/Strict-Transport-Security-768x193.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2022/05/Strict-Transport-Security-1536x385.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2022/05/Strict-Transport-Security-2048x514.jpg 2048w, https://thecodebuzz.com/wp-content/uploads/2022/05/Strict-Transport-Security-785x197.jpg 785w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>Today in this article, we shall see in ASP.NET Core how to Add HSTS Security Headers to Web/API applications.</p>



<p></p>



<p>Today we will cover the following aspects in this article,</p>



<p></p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#response-headers">Types of Security Headers</a></li><li><a href="#aioseo-enable-http-strict-transport-security-hsts-in-net-core">Enable HTTP Strict Transport Security (HSTS) in .NET Core</a></li><li><a href="#aioseo-characteristics-and-guidelines">Characteristics and guidelines</a></li><li><a href="#aioseo-adding-hsts-in-asp-net-core">Adding HSTS in ASP.NET Core</a><ul><li><a href="#aioseo-step-1">Step 1</a></li><li><a href="#aioseo-step-2">Step 2</a></li></ul></li><li><a href="#aioseo-usehttpsredirection-middleware">UseHttpsRedirection middleware</a></li><li><a href="#aioseo-additional-guidelines">Additional Guidelines</a></li></ul></div>



<p></p>



<p>Most of these headers once added to response headers use in-built browser features to protect your data and communication over the network.</p>



<p></p>



<h2 class="wp-block-heading" id="response-headers">Types of Security Headers </h2>



<p></p>



<p>Below are the various <strong><em>security headers which can be used in various contexts</em></strong> as needed. </p>



<p></p>



<ul class="wp-block-list">
<li><strong><em>HTTP Strict Transport Security (<a href="https://www.thecodebuzz.com/asp-net-core-security-headers-hsts-x-content-type-options-content-security-policy-x-xss-protection/" target="_blank" rel="noopener" title="ASP.NET Core Security Headers Guidelines">HSTS</a>)</em></strong></li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><strong><em>X-Frame-Options</em></strong></li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><strong><em>X-Content-Type-Options</em></strong></li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><strong><em>Content-Security-Policy</em></strong></li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><strong><em>X-XSS-Protection</em></strong></li>
</ul>



<p></p>



<h2 class="wp-block-heading has-medium-font-size" id="aioseo-enable-http-strict-transport-security-hsts-in-net-core">Enable HTTP Strict Transport Security (HSTS) in .NET Core </h2>



<p></p>



<div data-schema-only="false" class="wp-block-aioseo-faq" id="aioseo-net-core-3-0-and-lower-version-for-the-lower-version-of-net-core-like-net-core-2-2-and-lower-please-register-service-for-ihttpcontextaccessor-as-below-public-void-configureservicesiservicecollection-services-services-addmvc-setcompatibilityversioncompatibilityversion-version_2_2-services-addsingletonihttpcontextaccessor-httpcontextaccessor"><div class="aioseo-faq-block-question"><strong>What is HSTS (<em>HTTP Strict Transport Security</em> ) <br><br></strong>The HTTP <code>Strict-Transport-Security i.e.</code> HSTS informs browsers that the application or site should only be accessed via HTTPS protocol. This also indicates any access if done via HTTP should automatically be converted to HTTPS.<br></div><div class="aioseo-faq-block-answer">
<p></p>
</div></div>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-characteristics-and-guidelines"><em>Characteristics and guidelines</em></h2>



<p></p>



<p>This header provides below <em>characteristics</em> (but are not limited to), and <a href="https://www.thecodebuzz.com/asp-net-core-security-headers-hsts-x-content-type-options-content-security-policy-x-xss-protection/" target="_blank" rel="noopener" title="ASP.NET Core Security Headers Guidelines">guidelines for building secured</a> applications</p>



<p></p>



<ul class="wp-block-list">
<li><em>It allows the websites</em> or API owners<em> to declare their website/API is accessible only via <strong>secure </strong>connections.</em></li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><em>It allows the user of the website to interact with the website/API in secure connections.</em></li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><em>It protects the websites against protocol downgrade attacks. </em></li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><em>It protects the website&#8217;s action against cookie hijacking.</em></li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><em>It allows interaction with <strong>HTTPS </strong>connections only.</em></li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><em>The server implements <strong>Strict-Transport-Security</strong> by adding a header over an <strong>HTTPS </strong>connection.</em></li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><em><strong>HSTS </strong>Headers are ingonred over <strong>HTTP</strong>.</em></li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><em>The browser restricts the user from using untrusted or invalid certificates.&nbsp;</em></li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><em>The browser disables prompts that allow a user to temporarily trust such a certificate.</em></li>
</ul>



<p></p>



<h2 class="wp-block-heading" id="aioseo-adding-hsts-in-asp-net-core">Adding HSTS in ASP.NET Core</h2>



<p></p>



<p>Adding <strong><em>HSTS </em></strong>in ASP.NET Core can be achieved using the middleware component easily. We shall see both inline middleware and custom middleware techniques.</p>



<p></p>



<p><strong>Syntax :</strong></p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size"><em>Strict-Transport-Security: max-age=&lt;time&gt;
Strict-Transport-Security: max-age=&lt;time&gt;; includeSubDomains
Strict-Transport-Security: max-age= &lt;time&gt;; preload</em></pre>



<p></p>



<p><em>Directives details:</em></p>



<p></p>



<ul class="wp-block-list">
<li><strong>max-age=&lt;expire-time&gt; &#8211; (Required)</strong> &#8211; Time in seconds. This is the duration of time the browser remembers that a site is only to be accessed using HTTPS.</li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><strong>includeSubDomains &#8211; (Optional)</strong> &#8211; Enables include SubDomain parameter of the Strict-Transport-Security header.</li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><strong>preload- (Optional)</strong> &#8211; preload is supported by web browsers but is not part of the RFC specification yet.</li>
</ul>



<p></p>



<p><strong>Example</strong>:</p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size"> content-type: application/json; charset=utf-8 
 date: Sun24 Jan 2021 00:01:09 GMT 
 server: Microsoft-IIS/10.0 
 strict-transport-security: max-age=31536000 ; includeSubDomains;preload
 x-powered-by: ASP.NET </pre>



<p></p>



<p>In the above example,</p>



<p></p>



<ul class="wp-block-list">
<li> <em><strong>max-age</strong></em> is set to 1 year </li>



<li>suffixed with <code><strong>preload</strong></code>, (necessary for inclusion in all major web browsers&#8217; HSTS preload lists, like Chromium, Edge, and Firefox.)</li>
</ul>



<p></p>



<p><strong><em>Strict-Transport-Security</em></strong> can be added to ASP.NET Core API programmatically using the <strong><em>middleware </em></strong>approach which is discussed below in more detail.</p>



<p></p>



<p>The below code helps you add the HSTS middleware component to the API pipeline as below,</p>



<p></p>



<h3 class="wp-block-heading" id="aioseo-step-1"><strong>Step 1</strong></h3>



<p></p>



<p>In the ConfigureServices, using <strong><em>AddHsts </em></strong>which adds the required HSTS services</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; highlight: [6,7,8,9,10,11]; title: ; notranslate">
public void ConfigureServices(IServiceCollection services)
        {

            services.AddControllers();

            services.AddHsts(options =&gt;
            {
                options.Preload = true;
                options.IncludeSubDomains = true;
                options.MaxAge = TimeSpan.FromDays(365);
            });

..

..

       }
</pre></div>


<p></p>



<h3 class="wp-block-heading" id="aioseo-step-2"><strong>Step 2</strong></h3>



<p></p>



<p>In the <strong><em>Configure </em></strong>method add <strong><em>UseHsts </em></strong>middleware for using HSTS, which adds the Strict-Transport-Security header.</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; highlight: [10]; title: ; notranslate">
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();

            }
            else
            {
                app.UseHsts();

            }
..
..
..
      }
</pre></div>


<p></p>



<p><strong><em>The default HSTS value is 30 days if not specified. </em></strong></p>



<p></p>



<p>With the above basic steps, the ASP.NET Core application hosted on IIS or Clouds should be able to send HTTP Strict Transport Security Protocol (HSTS) headers to clients.</p>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="164" src="https://www.thecodebuzz.com/wp-content/uploads/2021/01/Adding-HSTS-in-ASP.NET-Core-1-1024x164.jpg" alt="Failed to determine the https port for redirect,ASP.NET Core How to Add HSTS" class="wp-image-14557" srcset="https://thecodebuzz.com/wp-content/uploads/2021/01/Adding-HSTS-in-ASP.NET-Core-1-1024x164.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/01/Adding-HSTS-in-ASP.NET-Core-1-300x48.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/01/Adding-HSTS-in-ASP.NET-Core-1-768x123.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/01/Adding-HSTS-in-ASP.NET-Core-1-1536x246.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2021/01/Adding-HSTS-in-ASP.NET-Core-1-2048x328.jpg 2048w, https://thecodebuzz.com/wp-content/uploads/2021/01/Adding-HSTS-in-ASP.NET-Core-1-785x126.jpg 785w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p>Using <strong><em>PostMan</em></strong>,</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="482" src="https://www.thecodebuzz.com/wp-content/uploads/2021/01/enforce-security-headers-HSTS-in-ASP.NET-Core-1-1-1024x482.jpg" alt="enforce security headers HSTS in ASP.NET Core " class="wp-image-14578" srcset="https://thecodebuzz.com/wp-content/uploads/2021/01/enforce-security-headers-HSTS-in-ASP.NET-Core-1-1-1024x482.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/01/enforce-security-headers-HSTS-in-ASP.NET-Core-1-1-300x141.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/01/enforce-security-headers-HSTS-in-ASP.NET-Core-1-1-768x361.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/01/enforce-security-headers-HSTS-in-ASP.NET-Core-1-1-1536x723.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2021/01/enforce-security-headers-HSTS-in-ASP.NET-Core-1-1-785x369.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2021/01/enforce-security-headers-HSTS-in-ASP.NET-Core-1-1.jpg 2021w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<h2 class="wp-block-heading" id="aioseo-usehttpsredirection-middleware">UseHttpsRedirection middleware</h2>



<p></p>



<p>As per Microsoft guidelines, the production ASP.NET Core app should also use HTTPS Redirection Middleware to redirect HTTP requests to HTTPS. </p>



<p></p>



<p>This can be done by adding <strong><em>UseHttpsRedirection</em></strong> a Redirection middleware in the API.</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; highlight: [9,10,11,12]; title: ; notranslate">
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseHsts();
    }

    app.UseHttpsRedirection();

    app.UseRouting();

    app.UseAuthorization();

    app.UseEndpoints(endpoints =&gt;
    {
        endpoints.MapRazorPages();
    });
}
</pre></div>


<p></p>



<p></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>Apps deployed with a reverse proxy and capable of handling HTTPS redirection don&#8217;t need to use HTTPS Redirection Middleware. </p>



<p></p>



<p>For more information please refer<strong> </strong><a href="https://docs.microsoft.com/en-us/aspnet/core/security/enforcing-ssl?view=aspnetcore-5.0&amp;tabs=visual-studio#opt-out-of-httpshsts-on-project-creation" target="_blank" rel="noreferrer noopener">to</a><strong><a href="https://docs.microsoft.com/en-us/aspnet/core/security/enforcing-ssl?view=aspnetcore-5.0&amp;tabs=visual-studio#opt-out-of-httpshsts-on-project-creation" target="_blank" rel="noreferrer noopener"><em> Microsoft guidelines </em></a></strong></p>
</blockquote>



<p></p>



<p>Redirection Middleware will need a port to be available to redirect an HTTP request to HTTPS else error &#8220;Failed to determine the HTTPS port for the redirect.&#8221; will be thrown.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-additional-guidelines">Additional Guidelines </h2>



<p></p>



<ul class="wp-block-list">
<li>Do not use <strong>HSTS </strong>in a local development environment. HSTS setting is recommended to be used in a Prod environment.</li>
</ul>



<p></p>



<ul class="wp-block-list">
<li>HSTS settings are cached by the browser.</li>
</ul>



<p></p>



<ul class="wp-block-list">
<li>HSTS settings are ignored over an HTTP connection.</li>
</ul>



<p></p>



<ul class="wp-block-list">
<li>HSTS excludes the below <strong>loopback host</strong>. Example
<ul class="wp-block-list">
<li><strong>localhost</strong></li>



<li><strong>127.0.0.1</strong></li>



<li><strong>[::1]</strong></li>
</ul>
</li>
</ul>



<p></p>



<p><strong><em>References: </em></strong></p>



<p></p>



<p>If you want to set various headers like <strong><em>X-Frame-Option</em></strong> <strong><em>X-Content-Type-Options</em></strong> <strong><em>Content-Security-Policy</em></strong> <strong><em>X-XSS-Protection</em></strong>, please visit the below article for more details,</p>



<p></p>



<ul class="wp-block-list">
<li><strong><em><a href="https://www.thecodebuzz.com/asp-net-core-security-headers-hsts-x-content-type-options-content-security-policy-x-xss-protection/" target="_blank" rel="noreferrer noopener" title="ASP.NET Core Security Headers Guidelines">Important Security Headers any application should use &#8211; Usage and Guidelines</a></em></strong></li>
</ul>



<p></p>



<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p><p>The post <a href="https://thecodebuzz.com/http-strict-transport-security-hsts-security-headers/">ASP.NET Core How to Add HSTS Security Headers</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/http-strict-transport-security-hsts-security-headers/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
		<item>
		<title>ASP.NET Core Security Headers Guidelines</title>
		<link>https://thecodebuzz.com/asp-net-core-security-headers-hsts-x-content-type-options-content-security-policy-x-xss-protection/</link>
					<comments>https://thecodebuzz.com/asp-net-core-security-headers-hsts-x-content-type-options-content-security-policy-x-xss-protection/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Wed, 28 Jul 2021 00:59:00 +0000</pubDate>
				<category><![CDATA[Secured Programming]]></category>
		<category><![CDATA[.net core x-frame-options]]></category>
		<category><![CDATA[add hsts header]]></category>
		<category><![CDATA[add security headers in asp net]]></category>
		<category><![CDATA[add xss headerasp.net core content-security-policy]]></category>
		<category><![CDATA[asp net core add header to all requests]]></category>
		<category><![CDATA[asp net core security middleware]]></category>
		<category><![CDATA[how to add default security headers in asp.net core]]></category>
		<category><![CDATA[how to set content-security-policy header in asp net mvc]]></category>
		<category><![CDATA[security headers in mvc]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=14559</guid>

					<description><![CDATA[<p>Today in this article, we shall cover ASP.NET Core Security Headers Guidelines. We will see how to enable security headers as part of security best practices protecting our ASP.NET Core API. I shall talk about more specific headers which are always good to have and recommended as per the OWASP specifications. These headers are simple [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/asp-net-core-security-headers-hsts-x-content-type-options-content-security-policy-x-xss-protection/">ASP.NET Core Security Headers Guidelines</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<figure class="wp-block-image size-large"><a href="https://www.thecodebuzz.com/asp-net-core-security-headers-hsts-x-content-type-options-content-security-policy-x-xss-protection/" target="_blank" rel="noopener"><img loading="lazy" decoding="async" width="1024" height="335" src="https://www.thecodebuzz.com/wp-content/uploads/2021/01/Add-HTTP-Strict-Transport-Security-HSTS-X-Content-Type-Options-Content-Security-Policy-X-XSS-Protection-.NET_-1024x335.jpg" alt="ASP.NET Core Security Headers " class="wp-image-14622" srcset="https://thecodebuzz.com/wp-content/uploads/2021/01/Add-HTTP-Strict-Transport-Security-HSTS-X-Content-Type-Options-Content-Security-Policy-X-XSS-Protection-.NET_-1024x335.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/01/Add-HTTP-Strict-Transport-Security-HSTS-X-Content-Type-Options-Content-Security-Policy-X-XSS-Protection-.NET_-300x98.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/01/Add-HTTP-Strict-Transport-Security-HSTS-X-Content-Type-Options-Content-Security-Policy-X-XSS-Protection-.NET_-768x251.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/01/Add-HTTP-Strict-Transport-Security-HSTS-X-Content-Type-Options-Content-Security-Policy-X-XSS-Protection-.NET_-1536x502.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2021/01/Add-HTTP-Strict-Transport-Security-HSTS-X-Content-Type-Options-Content-Security-Policy-X-XSS-Protection-.NET_-785x257.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2021/01/Add-HTTP-Strict-Transport-Security-HSTS-X-Content-Type-Options-Content-Security-Policy-X-XSS-Protection-.NET_.jpg 1977w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a></figure>



<p>Today in this article, we shall cover ASP.NET Core Security Headers Guidelines. We will see how to enable security headers as part of security best practices protecting our ASP.NET Core API.</p>



<p>I shall talk about more specific headers which are always good to have and recommended as per the <strong><em><a href="https://owasp.org/www-project-secure-headers/" target="_blank" rel="noreferrer noopener" title="https://owasp.org/www-project-secure-headers/">OWASP </a>specifications. </em></strong></p>



<p><em>These headers are simple to use and can be incorporated into your API or Web Application with simple easy-to configure steps</em>.</p>



<p>Most of these headers once added to response headers use inbuilt browser features to protect your data and communication over the network.</p>



<p></p>



<p>We will cover the below aspects in the article,</p>



<p></p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#response-headers">Response Headers</a></li><li><a href="#aioseo-http-strict-transport-security-hsts">HTTP Strict Transport Security (HSTS)</a><ul><li><a href="#aioseo-w">What is HSTS</a></li></ul></li><li><a href="#aioseo-x-content-type-options">X-Content-Type-Options</a></li><li><a href="#aioseo-content-security-policy">Content-Security-Policy</a></li><li><a href="#aioseo-x-xss-protection">X-XSS-Protection</a></li></ul></div>



<p></p>



<h2 class="wp-block-heading" id="response-headers">Response Headers</h2>



<p></p>



<p>Below are the various response headers which can be used in various contexts as needed.  We shall be covering a few, important basic headers in this article. </p>



<p></p>



<ul class="wp-block-list">
<li><strong><span class="has-inline-color has-luminous-vivid-orange-color">HTTP Strict Transport Security (HSTS)</span></strong></li>



<li>X-Frame-Options</li>



<li><span style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>X-Content-Type-Options</strong></span></li>



<li>X-Permitted-Cross-Domain-Policies</li>



<li>Referrer-Policy</li>



<li><strong><span class="has-inline-color has-luminous-vivid-orange-color">Content-Security-Policy</span></strong></li>



<li>Feature-Policy</li>



<li>Public Key Pinning Extension for HTTP (HPKP)</li>



<li>Expect-CT</li>



<li><strong><span class="has-inline-color has-luminous-vivid-orange-color">X-XSS-Protection</span></strong></li>
</ul>



<p></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>Out of all the above response headers, one can very much use highlighted headers in most of the use cases. Having above-highlighted headers are always good to have and help our API or Website secured well enough.</p>
</blockquote>



<p></p>



<p>Let&#8217;s get started.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-http-strict-transport-security-hsts"><strong>HTTP Strict Transport Security (HSTS)</strong></h2>



<p></p>



<div data-schema-only="false" class="wp-block-aioseo-faq" id="aioseo-w"><h3 class="aioseo-faq-block-question">What is HSTS</h3><div class="aioseo-faq-block-answer">
<p>HSTS (<strong>HTTP Strict Transport Security</strong>)  is an IETF standard, Strict Transport Security protocol, and is as per specifications and standards specified in <strong><a href="https://tools.ietf.org/html/rfc6797#section-2.1" target="_blank" rel="noreferrer noopener">RFC 6797</a>.</strong> It allows the web sites owner to declare their website is accessible only via secure connections. It allows the u<em>ser of the website</em> to interact with the website in secure connections. </p>
</div></div>



<p></p>



<p>HSTS is an IETF standard, Strict Transport Security protocol, and is as per specification and standards specified in <strong><a href="https://tools.ietf.org/html/rfc6797#section-2.1" target="_blank" rel="noreferrer noopener">RFC 6797</a>.</strong> It allows the web sites owner to declare their website is accessible only via secure connections. It allows the u<em>ser of the website</em> to interact with the website in secure connections.</p>



<p></p>



<p><strong>Syntax :</strong></p>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow" style="flex-basis:100%">
<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>Strict-Transport-Security: max-age=<br>Strict-Transport-Security: max-age=; includeSubDomains<br>Strict-Transport-Security: max-age=; preload</p>
</blockquote>
</div>
</div>



<p></p>



<p>Enabling <strong><em>HSTS in ASP.NET Core is simple and it is explained in detail</em></strong> in the below article.</p>



<p><strong><em>Example</em></strong></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; highlight: [6,7,8,9,10,11,26]; title: ; notranslate">
 public void ConfigureServices(IServiceCollection services)
        {

            services.AddControllers();

            services.AddHsts(options =&gt;
            {
                options.Preload = true;
                options.IncludeSubDomains = true;
                options.MaxAge = TimeSpan.FromDays(365);
            });
&#039;&#039;
&#039;&#039;
&#039;&#039;
         }

//
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

</pre></div>


<p></p>



<p>For more details, please visit this article,</p>



<p></p>



<ul class="wp-block-list">
<li><strong><em><a href="https://www.thecodebuzz.com/http-strict-transport-security-hsts-security-headers/" title="ASP.NET Core HSTS Security Headers Guidelines" target="_blank" rel="noreferrer noopener">Add HSTS Security Header</a></em></strong><strong><em><a href="https://www.thecodebuzz.com/http-strict-transport-security-hsts-security-headers/" title="ASP.NET Core HSTS Security Headers Guidelines" target="_blank" rel="noreferrer noopener">s</a></em></strong><strong><em><a href="https://www.thecodebuzz.com/http-strict-transport-security-hsts-security-headers/" title="ASP.NET Core HSTS Security Headers Guidelines" target="_blank" rel="noreferrer noopener"> </a></em></strong><strong><em><a href="https://www.thecodebuzz.com/http-strict-transport-security-hsts-security-headers/" title="ASP.NET Core HSTS Security Headers Guidelines" target="_blank" rel="noreferrer noopener">A</a></em></strong><strong><em><a href="https://www.thecodebuzz.com/http-strict-transport-security-hsts-security-headers/" title="ASP.NET Core HSTS Security Headers Guidelines" target="_blank" rel="noreferrer noopener">S</a></em></strong><strong><em><a href="https://www.thecodebuzz.com/http-strict-transport-security-hsts-security-headers/" title="ASP.NET Core HSTS Security Headers Guidelines" target="_blank" rel="noreferrer noopener">P</a></em></strong><strong><em><a href="https://www.thecodebuzz.com/http-strict-transport-security-hsts-security-headers/" title="ASP.NET Core HSTS Security Headers Guidelines" target="_blank" rel="noreferrer noopener">.</a></em></strong><strong><em><a href="https://www.thecodebuzz.com/http-strict-transport-security-hsts-security-headers/" title="ASP.NET Core HSTS Security Headers Guidelines" target="_blank" rel="noreferrer noopener">N</a></em></strong><strong><em><a href="https://www.thecodebuzz.com/http-strict-transport-security-hsts-security-headers/" title="ASP.NET Core HSTS Security Headers Guidelines" target="_blank" rel="noreferrer noopener">E</a></em></strong><strong><em><a href="https://www.thecodebuzz.com/http-strict-transport-security-hsts-security-headers/" title="ASP.NET Core HSTS Security Headers Guidelines" target="_blank" rel="noreferrer noopener">T</a></em></strong><strong><em><a href="https://www.thecodebuzz.com/http-strict-transport-security-hsts-security-headers/" title="ASP.NET Core HSTS Security Headers Guidelines" target="_blank" rel="noreferrer noopener"> </a></em></strong><strong><em><a href="https://www.thecodebuzz.com/http-strict-transport-security-hsts-security-headers/" title="ASP.NET Core HSTS Security Headers Guidelines" target="_blank" rel="noreferrer noopener">C</a></em></strong><strong><em><a href="https://www.thecodebuzz.com/http-strict-transport-security-hsts-security-headers/" title="ASP.NET Core HSTS Security Headers Guidelines" target="_blank" rel="noreferrer noopener">o</a></em></strong><strong><em><a href="https://www.thecodebuzz.com/http-strict-transport-security-hsts-security-headers/" title="ASP.NET Core HSTS Security Headers Guidelines" target="_blank" rel="noreferrer noopener">r</a></em></strong><a href="https://www.thecodebuzz.com/http-strict-transport-security-hsts-security-headers/" title="ASP.NET Core HSTS Security Headers Guidelines" target="_blank" rel="noreferrer noopener"><strong><em>e</em></strong></a></li>
</ul>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-x-content-type-options"><strong>X-Content-Type-Options</strong></h2>



<p></p>



<p>This is one of the headers which secures the content type of the data communicated. This header disables the wrong or malicious interpretation of <strong><em>Content-Type</em></strong>.</p>



<p>This header has only one value &#8220;<em><strong>nosniff</strong></em>&#8221; i.e do not sniff the content type and choose the only content type specified by the application via <strong><em>Content-Type</em></strong>.</p>



<p></p>



<p><strong>Syntax </strong></p>



<p></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>X-Content-Type-Options: nosniff</p>
</blockquote>



<p></p>



<p>Add <strong>X-Content-Type-Options</strong> header in ASP.NET Core using middleware as below,</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; highlight: [14,15,16,17,18]; title: ; notranslate">
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

            app.UseHttpsRedirection();

            app.Use(async (context, next) =&gt;
            {
                context.Response.Headers.Add(&quot;X-Content-Type-Options&quot;, &quot;nosniff&quot;);
                await next.Invoke();
            });
..
..
     }



</pre></div>


<p></p>



<h2 class="wp-block-heading" id="aioseo-content-security-policy"><strong>Content-Security-Policy</strong></h2>



<p></p>



<p>A <strong>Content-Security-Policy</strong> (<strong><em>CSP) </em></strong>header enables you to control the sources/content on your site that the browser can load. So this header gives you the ability to load the only resources needed by the browser. </p>



<p>A Content Security Policy (<strong>CSP</strong>) helps protect against XSS attacks by informing the browser of valid re-sources like as below,</p>



<ul class="wp-block-list">
<li>Content, scripts, stylesheets, and images.</li>



<li>Actions are taken by a page, specifying permitted URL targets of forms.</li>



<li>Plugins that can be loaded.</li>
</ul>



<p></p>



<p><strong>Syntax </strong></p>



<p></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><strong>Content-Security-Policy: default-src ‘self’</strong> </p>
</blockquote>



<p></p>



<p>Add <strong><strong>Content-Security-Policy</strong></strong> header in ASP.NET Core using middleware as below,</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; highlight: [14,15,16,17,18]; title: ; notranslate">
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

            app.UseHttpsRedirection();

            app.Use(async (context, next) =&gt;
            {
                context.Response.Headers.Add(&quot;Content-Security-Policy&quot;, &quot;default-src &#039;self&#039;;&quot;);
                await next.Invoke();
            });
    ..
    .. 
       }

</pre></div>


<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" width="1024" height="531" src="https://www.thecodebuzz.com/wp-content/uploads/2021/01/Adding-Content-Security-Policy-in-ASP.NET-Core-1024x531.jpg" alt="" class="wp-image-14597" style="width:517px;height:267px" srcset="https://thecodebuzz.com/wp-content/uploads/2021/01/Adding-Content-Security-Policy-in-ASP.NET-Core-1024x531.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/01/Adding-Content-Security-Policy-in-ASP.NET-Core-300x155.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/01/Adding-Content-Security-Policy-in-ASP.NET-Core-768x398.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/01/Adding-Content-Security-Policy-in-ASP.NET-Core-1536x796.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2021/01/Adding-Content-Security-Policy-in-ASP.NET-Core-785x407.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2021/01/Adding-Content-Security-Policy-in-ASP.NET-Core.jpg 1993w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<h2 class="wp-block-heading" id="aioseo-x-xss-protection"><strong>X-XSS-Protection</strong></h2>



<p></p>



<p><strong>X-XSS-Protection</strong> header is for protecting your site from <strong><em>XSS (Cross-site scripting) attacks</em></strong>. If a<strong><em> cross-site scripting</em></strong> attack is detected, the browser will sanitize the page and the malicious part will either be removed OR the browser will prevent rendering of the page and will block an attack (<strong>mode=block</strong>).</p>



<p></p>



<p><strong>Syntax</strong></p>



<p></p>



<p>Below are the four options for enabling Cross-site scripting.</p>



<p></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><code>X-XSS-Protection: 0 </code> </p>



<p><code>X-XSS-Protection: 1 </code></p>



<p> <code>X-XSS-Protection: 1; mode=block </code> </p>



<p><code>X-XSS-Protection: 1; report=&lt;report-uri&gt;</code></p>
</blockquote>



<p></p>



<p>Add <strong>X-XSS-Protection</strong> header in ASP.NET Core using middleware as below,</p>



<p></p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" width="1024" height="527" src="https://www.thecodebuzz.com/wp-content/uploads/2021/01/Adding-X-XSS-Protection-in-ASP.NET-Core-1024x527.jpg" alt="Add X-XSS-Protection header" class="wp-image-14612" style="width:530px;height:272px" srcset="https://thecodebuzz.com/wp-content/uploads/2021/01/Adding-X-XSS-Protection-in-ASP.NET-Core-1024x527.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/01/Adding-X-XSS-Protection-in-ASP.NET-Core-300x154.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/01/Adding-X-XSS-Protection-in-ASP.NET-Core-768x395.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/01/Adding-X-XSS-Protection-in-ASP.NET-Core-1536x791.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2021/01/Adding-X-XSS-Protection-in-ASP.NET-Core-785x404.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2021/01/Adding-X-XSS-Protection-in-ASP.NET-Core.jpg 1772w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<p>After adding <strong><em>all headers </em></strong>together in the middleware component and hosting it cloud below is how we can visualize all the response headers,</p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" width="1024" height="558" src="https://www.thecodebuzz.com/wp-content/uploads/2021/01/HTTP-Strict-Transport-Security-HSTS-X-Content-Type-Options-Content-Security-Policy-X-XSS-Protection-to-ASP.NET-Core-1024x558.jpg" alt="Content Security Policy (CSP)" class="wp-image-14617" style="width:525px;height:286px" srcset="https://thecodebuzz.com/wp-content/uploads/2021/01/HTTP-Strict-Transport-Security-HSTS-X-Content-Type-Options-Content-Security-Policy-X-XSS-Protection-to-ASP.NET-Core-1024x558.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/01/HTTP-Strict-Transport-Security-HSTS-X-Content-Type-Options-Content-Security-Policy-X-XSS-Protection-to-ASP.NET-Core-300x163.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/01/HTTP-Strict-Transport-Security-HSTS-X-Content-Type-Options-Content-Security-Policy-X-XSS-Protection-to-ASP.NET-Core-768x418.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/01/HTTP-Strict-Transport-Security-HSTS-X-Content-Type-Options-Content-Security-Policy-X-XSS-Protection-to-ASP.NET-Core-1536x837.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2021/01/HTTP-Strict-Transport-Security-HSTS-X-Content-Type-Options-Content-Security-Policy-X-XSS-Protection-to-ASP.NET-Core-785x428.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2021/01/HTTP-Strict-Transport-Security-HSTS-X-Content-Type-Options-Content-Security-Policy-X-XSS-Protection-to-ASP.NET-Core.jpg 1823w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="459" src="https://www.thecodebuzz.com/wp-content/uploads/2021/01/Add-HTTP-Strict-Transport-Security-HSTS-X-Content-Type-Options-Content-Security-Policy-X-XSS-Protection-1024x459.jpg" alt="" class="wp-image-14618" srcset="https://thecodebuzz.com/wp-content/uploads/2021/01/Add-HTTP-Strict-Transport-Security-HSTS-X-Content-Type-Options-Content-Security-Policy-X-XSS-Protection-1024x459.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/01/Add-HTTP-Strict-Transport-Security-HSTS-X-Content-Type-Options-Content-Security-Policy-X-XSS-Protection-300x134.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/01/Add-HTTP-Strict-Transport-Security-HSTS-X-Content-Type-Options-Content-Security-Policy-X-XSS-Protection-768x344.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/01/Add-HTTP-Strict-Transport-Security-HSTS-X-Content-Type-Options-Content-Security-Policy-X-XSS-Protection-1536x688.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2021/01/Add-HTTP-Strict-Transport-Security-HSTS-X-Content-Type-Options-Content-Security-Policy-X-XSS-Protection-2048x917.jpg 2048w, https://thecodebuzz.com/wp-content/uploads/2021/01/Add-HTTP-Strict-Transport-Security-HSTS-X-Content-Type-Options-Content-Security-Policy-X-XSS-Protection-785x352.jpg 785w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<p><strong><em>References</em></strong>:</p>



<ul class="wp-block-list">
<li><a href="https://www.thecodebuzz.com/http-strict-transport-security-hsts-security-headers/" target="_blank" rel="noreferrer noopener" title="ASP.NET Core HSTS Security Headers Guidelines"><strong><em>ASP.NET Core HSTS Security Headers Guidelines</em></strong></a></li>
</ul>



<p></p>



<p>That&#8217;s all, Enjoy Coding!</p>



<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p><p>The post <a href="https://thecodebuzz.com/asp-net-core-security-headers-hsts-x-content-type-options-content-security-policy-x-xss-protection/">ASP.NET Core Security Headers Guidelines</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/asp-net-core-security-headers-hsts-x-content-type-options-content-security-policy-x-xss-protection/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
