36

When you initially set up IIS Express to enable SSL, it defaults the port to 44300. Unfortunately, when I try to access my site in on https://localhost/ it doesn't work unless I use the port number 44300 - https://localhost:44300/.

The links are generated using the following:

<%= Html.ActionLink("Index", "Index", "Home", new { @action = "https://" + Request.Hostname + Url.Action("Index", "Home") }) %>

Although an ugly solution, the @action keyword can override the generated route, but it means that the application would seemingly need to be aware of any non-standard ports (eg 44300).

The problem with that is that I'd be writing something to solve a problem that would only occur in a development environment.

So my question is... How do I change the port to 443 and have IIS Express like it?

Config for my site is below:

<site name="MySite" id="2" serverAutoStart="true">
  <application path="/">
    <virtualDirectory path="/" physicalPath="C:\Inetpub\MySite" />
  </application>
  <bindings>
    <binding protocol="http" bindingInformation=":80:" />
    <binding protocol="https" bindingInformation=":44300:" />
  </bindings>
</site>

Many thanks in advance.

Update:

This question has been answered by Divya over on the IIS forums.

Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114

5 Answers5

28

This question has been answered by Divya over on the IIS forums.

Once you enable SSL for a website in WebMatrix, it defaults to port 44300 and does all the bindings in the background. I am hoping that you tried to change this port to 443 in the config file. Once that is done and saved, you also need to modify the binding in http.sys. You would need to delete the existing entry for port 44300 and add the entry for port 443. To do this, you could use httpcfg (WinXp/Win2003) or 'netsh http' (WinVista/Win2K8/Win7). Here are the commands for netsh:

1) Get the appid and certhash for the existing entry of 44300 (I assume, you are going to use the same certificate which WebMatrix installs by default. If you want to change the certificate as well, get the certificate hash of the certificate from the certificate store): netsh http show sslcert. In the output search for entry for port 44300 and copy certhash and appID.

2) Delete the entry for 44300: netsh http delete sslcert ipport=0.0.0.0:44300

3) Add a new entry for port 443 with certhash and appID copied in step 1. netsh http add sslcert ipport=0.0.0.0:443 certhash=<certhash> appid=<appid>

After configuring the entry in http.sys, you need to restart http service for the changes to take effect.

net stop http

net start http

As noted by others, there are several nice ways of getting your SSL certs.

netsh http show sslcert > output.txt

or (my preferred method):

netsh http show sslcert | clip
Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
  • 1
    on my Win7 machine, `netsh http show sslcert` returned over 100 entries and 44300 was at the top. so use commands `netsh http show sslcert | more` or `netsh http show sslcert > output.txt` to make things easier on yourself. – rymo Mar 28 '13 at 21:03
  • 3
    @rymo As a side approach, you may use `netsh http show sslcert ipport=0.0.0.0:44300` which will filter down to that specific one. – Alpha Sep 25 '13 at 19:33
  • 1
    or another solution: `netsh http show sslcert | clip` to copy the result to clipboard then paste it in your text editor and search there – Mariusz Pawelski Mar 17 '15 at 16:25
  • If you get 'the parameter is incorrect' in this step 3) in Dan's this post, try netsh http add sslcert ipport=0.0.0.0:44300 appid={appid} certhash= see http://stackoverflow.com/questions/779228/the-parameter-is-incorrect-error-using-netsh-http-add-sslcert – Danniel Little Oct 12 '16 at 13:33
4

Since I have spent much time on this topic , I would like to share my finding. I am reposting segment from my other post minus the code. Some background and explanation:

==========================================

After researching aroud, I was able to solve this issue with IIS Express and an override of the Controller class's OnAuthorization method (Ref#1). I have also gone with the route recommended by Hanselman (Ref#2). However, I was not complete satisfied with these two solutions due to two reasons:

  1. Ref#1's OnAuthorization only works at the action level, not at the controller class level
  2. Ref#2 requires a lot of setup (Win7 SDK for makecert), netsh commands, and, in order to use port 80 and port 443, I need to launch VS2010 as administrator, which I frown upon.

So, I came up with this solution that is quite simplistic with the following conditions:

  1. I want to be able to use the RequireHttps attribute at Controller class or action level

  2. I want MVC to use HTTPS when the RequireHttps attribute is present, and use HTTP if it is absent

  3. I do not want to have to run Visual Studio as administrator

  4. I want to be able to use any HTTP and HTTPS ports that are assigned by IIS Express

  5. I can reuse the self-signed SSL cert of IIS Express, and I do not care if I see the invalid SSL Prompt

=========================================

You can find my solution/code here ==> ASP.NET MVC RequireHttps in Production Only

Community
  • 1
  • 1
Leng Keng
  • 71
  • 3
3

The port 44300 is sequential: 00 mean that its the first application you have configured as SSL enabled; 01 will be the second one and so on.

Since I also require my website to only work in HTTPS by adding the [RequireHttps] global attribute, I had some trouble debugging. When launched, it was automatically redirecting to https://localhost/

To fix this problem when debugging a web site, I simply create a new RequireHttpsAttribute that specify the port

#if DEBUG
public class RequireHttpsAttribute : System.Web.Mvc.RequireHttpsAttribute
{
    protected override void HandleNonHttpsRequest(System.Web.Mvc.AuthorizationContext filterContext)
    {
        base.HandleNonHttpsRequest(filterContext);

        var result = (RedirectResult)filterContext.Result;

        var uri = new UriBuilder(result.Url);
        uri.Port = 44301;

        filterContext.Result = new RedirectResult(uri.ToString());
    }
}
#endif

Use this class when debugging only. When deployed to IIS7, you should use Url rewriting to redirect to HTTPS.

Pierre-Alain Vigeant
  • 22,635
  • 8
  • 65
  • 101
  • It's this very thing that I wanted to avoid - having code that tries to handle this situation. In the end, I moved to Windows 7 and IIS 7, effectively removing my need for this hack. – Dan Atkinson Aug 06 '12 at 10:48
  • 2
    Sure, when deploying, you rely on proved and trusted method already provided by IIS, but when debugging, it is useful. – Pierre-Alain Vigeant Aug 07 '12 at 02:01
  • Absolutely. I would possibly add debug regions into the code in order to avoid non-production code being released. – Dan Atkinson Aug 07 '12 at 09:50
  • 1
    I'm adding the #if DEBUG to put an emphasis on the debug-only part – Pierre-Alain Vigeant Aug 07 '12 at 20:04
  • This idea can't be used for team development or development on different machines: The used port is not persisted in solution- or project- config files, only inside local IIS-Express installation. – springy76 Aug 29 '12 at 11:48
  • @springy76 are you sure, because I can clearly see `44301` in my project file? – Pierre-Alain Vigeant Aug 29 '12 at 13:08
  • @Pierre-AlainVigeant yes, absolutely sure since I'm using version control and there were no changes in .sln or .csproj files -- not even the .csproj.user file has been changed. I can close VS and restart und the SSL settings are still the same --just no indication of port 44300 although it's working well and definitely using IIS-Express. – springy76 Aug 29 '12 at 14:05
0

Dan answer is right but if you still have problems with configuring IIS Express to serve your website with http and https on standard ports here is nice tutorial that that guide you step by step:

http://www.lansweeper.com/kb/54/How-to-configure-SSL-in-IIS-Express.html

In my case I accidentally deleted IIS Express certificate. I think it is generated the first time you use SSL in Visual Studio (F4 on selected project to get properties window and checking 'SSS Enabled' checkbox). This tutorial guided me how to create certificate and fix it.

Mariusz Pawelski
  • 25,983
  • 11
  • 67
  • 80
0

Create class

public class RequireSSLAttribute: RequireHttpsAttribute
    {
        protected override void HandleNonHttpsRequest(AuthorizationContext filterContext)
        {
            base.HandleNonHttpsRequest(filterContext);

             if (filterContext.HttpContext.Request.Url.Host.ToLower().Equals("localhost"))
            {
                // redirect to HTTPS version of page
                string localhostSSLPort = System.Configuration.ConfigurationManager.AppSettings["localhostSSLPort"];
                string url = "https://" + filterContext.HttpContext.Request.Url.Host + ":" + localhostSSLPort + filterContext.HttpContext.Request.RawUrl;
                filterContext.Result = new RedirectResult(url);
            }           
        }
    }

And inside your web config add something like this

<appSettings>    
    <add key="localhostSSLPort" value="44300"/>
  </appSettings>

And then you use it like

    [RequireSSL]            
    public class AdminController : Controller
    {
              ...
    }
Alex Vaghin
  • 295
  • 1
  • 2
  • 6