简体   繁体   English

启用S​​SL时,IIS Express默认为端口44300 for HTTPS

[英]IIS Express defaulting to port 44300 for HTTPS when enabling SSL

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/ . 当您最初设置IIS Express以启用SSL时,它会将端口默认为44300.不幸的是,当我尝试访问https://localhost/上的站点时,除非我使用端口号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). 虽然是一个丑陋的解决方案,但@action关键字可以覆盖生成的路由,但这意味着应用程序似乎需要知道任何非标准端口(例如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? 所以我的问题是...... 如何将端口更改为443并让IIS Express像这样?

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. Divya已经在IIS论坛上回答了这个问题。

This question has been answered by Divya over on the IIS forums. Divya已经在IIS论坛上回答了这个问题。

Once you enable SSL for a website in WebMatrix, it defaults to port 44300 and does all the bindings in the background. 在WebMatrix中为网站启用SSL后,默认为端口44300并在后台执行所有绑定。 I am hoping that you tried to change this port to 443 in the config file. 我希望您尝试在配置文件中将此端口更改为443。 Once that is done and saved, you also need to modify the binding in http.sys. 完成并保存后,您还需要修改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). 您需要删除端口44300的现有条目并添加端口443的条目。为此,您可以使用httpcfg(WinXp / Win2003)或'netsh http'(WinVista / Win2K8 / Win7)。 Here are the commands for netsh: 以下是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 . 1)获取44300的现有条目的appid和certhash(我假设您将使用默认安装WebMatrix的相同证书。如果您还想更改证书,请从证书中获取证书的哈希值证书商店): netsh http show sslcert In the output search for entry for port 44300 and copy certhash and appID. 在输出搜索端口44300的条目和复制certhash和appID。

2) Delete the entry for 44300: netsh http delete sslcert ipport=0.0.0.0:44300 2)删除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> 3)在步骤1中复制certhash和appID为端口443添加新条目.netsh 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. 在http.sys中配置条目后,需要重新启动http服务才能使更改生效。

net stop http

net start http

As noted by others, there are several nice ways of getting your SSL certs. 正如其他人所指出的,有几种很好的方法可以获得SSL证书。

netsh http show sslcert > output.txt

or (my preferred method): 或(我的首选方法):

netsh http show sslcert | clip

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). 在研究了aroud之后,我能够用IIS Express解决这个问题并覆盖Controller类的OnAuthorization方法(Ref#1)。 I have also gone with the route recommended by Hanselman (Ref#2). 我也选择了Hanselman推荐的路线(参考文献#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 Ref#1的OnAuthorization仅适用于操作级别,而不适用于控制器级别
  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. Ref#2需要大量的设置(用于makecert Win7 SDK), netsh命令,并且为了使用端口80和端口443,我需要以管理员身份启动VS2010,我不赞成。

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 我希望能够在Controller类或操作级别使用RequireHttps属性

  2. I want MVC to use HTTPS when the RequireHttps attribute is present, and use HTTP if it is absent 我希望MVC在RequireHttps属性存在时使用HTTPS,如果不存在则使用HTTP

  3. I do not want to have to run Visual Studio as administrator 我不想以管理员身份运行Visual Studio

  4. I want to be able to use any HTTP and HTTPS ports that are assigned by IIS Express 我希望能够使用IIS Express分配的任何HTTP和HTTPS端口

  5. I can reuse the self-signed SSL cert of IIS Express, and I do not care if I see the invalid SSL Prompt 我可以重用IIS Express的自签名SSL证书,如果看到无效的SSL提示,我不在乎

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

You can find my solution/code here ==> ASP.NET MVC RequireHttps in Production Only 您可以在这里找到我的解决方案/代码==> 仅限生产中的ASP.NET MVC RequireHttps

The port 44300 is sequential: 00 mean that its the first application you have configured as SSL enabled; 端口44300是顺序的:00表示它是您配置为启用SSL的第一个应用程序; 01 will be the second one and so on. 01将是第二个,依此类推。

Since I also require my website to only work in HTTPS by adding the [RequireHttps] global attribute, I had some trouble debugging. 由于我还要求我的网站只通过添加[RequireHttps]全局属性来使用HTTPS,因此调试时遇到了一些问题。 When launched, it was automatically redirecting to https://localhost/ 启动时,它会自动重定向到https://localhost/

To fix this problem when debugging a web site , I simply create a new RequireHttpsAttribute that specify the port 在调试网站时解决此问题,我只需创建一个指定端口的新RequireHttpsAttribute

#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. 部署到IIS7时,您应该使用Url重写来重定向到HTTPS。

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: Dan的答案是正确的,但是如果您仍然在配置IIS Express以在标准端口上使用http和https为您的网站提供服务时遇到问题,那么这是一个很好的教程,可以指导您一步一步:

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

In my case I accidentally deleted IIS Express certificate. 在我的情况下,我不小心删除了IIS Express证书。 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). 我认为它是在Visual Studio中第一次使用SSL时生成的(所选项目中的F4获取属性窗口并选中“启用SSS”复选框)。 This tutorial guided me how to create certificate and fix it. 本教程指导我如何创建证书并修复它。

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 在你的web配置里面添加这样的东西

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

And then you use it like 然后你就像使用它一样

    [RequireSSL]            
    public class AdminController : Controller
    {
              ...
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM