Skip to main content

implement hsts iis

We can implement HSTS using multiple approaches.

Approach 1:

PRE-REQUISITES: URL Rewrite module has to be installed from the below link


Add the below code to web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="HTTP to HTTPS redirect" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}"
                        redirectType="Permanent" />
                </rule>
            </rules>
            <outboundRules>
                <rule name="Add Strict-Transport-Security when HTTPS" enabled="true">
                    <match serverVariable="RESPONSE_Strict_Transport_Security"
                        pattern=".*" />
                    <conditions>
                        <add input="{HTTPS}" pattern="on" ignoreCase="true" />
                    </conditions>
                    <action type="Rewrite" value="max-age=31536000; includeSubDomains; preload"  />
                </rule>
            </outboundRules>
        </rewrite>
    </system.webServer>
</configuration>

Approach 2:
Add the below code in global.asax
protected void Application_BeginRequest()
{
    switch (Request.Url.Scheme)
    {
        case "https":
            Response.AddHeader("Strict-Transport-Security", "max-age=31536000");
            break;
        case "http":
            var path = "https://" + Request.Url.Host + Request.Url.PathAndQuery;
            Response.Status = "301 Moved Permanently";
            Response.AddHeader("Location", path);
            break;
    }
}

Approach 3:
This is one of the simplest methods, but has a lot of limitations and ideally not used. Here is how we do it:

PRE-REQUISITES: HTTP Redirect module is installed and the website has a valid HTTPS binding in place.

Launch the IIS Manager.
Go to the HTTP Redirect module.
Fill the details as per the requirement as shown below:


We have to add HTTP Response Headers separately as explained below


Click on HTTP Response Headers
Click on Add... in the Actions panel.
Enter the following values in the Add Custom HTTP Response Headers dialog box :<
Name: Strict-Transport-Security
Value: max-age=31536000
Close the IIS Manager after confirmation.

Testing HSTS:

Method 1:

We can check HSTS status using Qualys SSL Labs.
Go to the below url

Inside the Hostname textbox, paste the required website to be tested for HSTS.
Besides the overall score, which is calculated based on a variety of indexes, we need to scroll the result page, once the analysis is completed, down to Protocol Details subsection and locate Strict Transport Security (HSTS) item in front of which there would be the actual result of checking against HSTS.


If HSTS is enabled then, it will display HSTS Yes with age as shown in above figure.

Method 2:

To test the result of our new header we simply reload our website using a non-secure URL. Upon inspecting the Network tab within the Developer Console in Chrome we can see that the browser is issuing a 307 internal redirect or 301 moved permanently.

MAX-AGE IN HSTS:

HSTS settings include a “max-age” option, which tells the browser how long to cache and remember the settings before checking again.

Testing Max-age:
Type chrome://net-internals/#hsts in chrome browser, it will display the below screen.

Under Query domain section, enter Domain name and click on Query it will display HSTS details for that particular site as shown below.

Comments

Popular posts from this blog

how to integrate kendo ui in asp.net mvc

Introduction: Here I am going to explain how to integrate Kendo to MVC5. I have developed this project by taking reference by this link http://docs.telerik.com/aspnet-mvc/getting-started/asp-net-mvc-5 Before integration, let’s be ready with Kendo UI stuff. Check in your machine, whether “ui-for-asp.net-mvc” installed or not. See the below screen shot If it is not there, then follow the below steps to install it. Step 1: Goto https://www.telerik.com/account/ If you don’t have account, you can create for trail account. Step 2: Paste the below link in browser once you have logged in https://www.telerik.com/download-trial-file/v2/ui-for-asp.net-mvc it will ask download location for software. It looks like below Once after completion of the software, install it. Now we are ready with all the files required for integration of Kendo with MVC. Follow the steps below Step 1: Open visual studio, sele