启用 SSL 时,IIS Express 默认使用 HTTPS 端口 44300

发布于 2024-09-18 21:45:45 字数 1108 浏览 8 评论 0原文

当您最初设置 IIS Express 以启用 SSL 时,它会将端口默认为 44300。不幸的是,当我尝试在 https://localhost/ 上访问我的网站时,它无法工作,除非我使用端口号 44300 - https://localhost:44300/

这些链接是使用以下内容生成的:

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

虽然是一个丑陋的解决方案,但 @action 关键字可以覆盖生成的路由,但这意味着应用程序似乎需要了解任何非标准端口(例如44300)。

问题是我要编写一些东西来解决仅在开发环境中出现的问题。

所以我的问题是... 如何将端口更改为 443 并拥有类似的 IIS Express?

我的网站配置如下:

<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>

非常感谢。

更新:

此问题已由 Divya 回答 IIS 论坛。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

醉酒的小男人 2024-09-25 21:45:46

创建类

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);
            }           
        }
    }

并在您的网络配置中添加类似的内容

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

然后您可以像这样使用它

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

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
    {
              ...
    }
扛起拖把扫天下 2024-09-25 21:45:45

Divya 在 IIS 论坛上回答了这个问题。

在 WebMatrix 中为网站启用 SSL 后,它默认使用端口 44300 并在后台执行所有绑定。我希望您尝试在配置文件中将此端口更改为 443。完成并保存后,您还需要修改 http.sys 中的绑定。您需要删除端口 44300 的现有条目并添加端口 443 的条目。
为此,您可以使用 httpcfg (WinXp/Win2003) 或“netsh http”(WinVista/Win2K8/Win7)。
以下是 netsh 的命令:

1) 获取现有条目 44300 的 appid 和 certhash (I
假设您将使用与 WebMatrix 相同的证书
默认安装。如果您也想更改证书,
从证书中获取证书的证书哈希
商店):netsh http show sslcert。在输出中搜索条目
端口 44300 并复制 certhash 和 appID。

2) 删除 44300 的条目:netsh http delete sslcert ipport=0.0.0.0:44300

3) 为端口 443 添加一个新条目,其中包含步骤中复制的 certhash 和 appID
1. netsh http add sslcert ipport=0.0.0.0:443 certhash=; appid=

配置http.sys中的条目后,需要重新启动http
服务以使更改生效。

net stop http

网络启动http

正如其他人所指出的,有几种获取 SSL 证书的好方法。

netsh http show sslcert > output.txt

或(我的首选方法):

netsh http show sslcert | clip

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
心房的律动 2024-09-25 21:45:45

由于我在这个主题上花了很多时间,我想分享我的发现。我正在重新发布我的另一篇文章中的片段,减去代码。一些背景和解释:

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

经过研究后,我能够使用 IIS Express 和重写 Controller 类的 OnAuthorization 方法 (Ref#1) 解决此问题。我也选择了 Hanselman 推荐的路线(参考#2)。然而,由于两个原因,我对这两个解决方案并不完全满意:

  1. Ref#1 的 OnAuthorization 仅适用于操作级别,不适用于控制器类级别
  2. Ref#2 需要大量设置(Win7 SDK for makecert)、netsh 命令,并且为了使用端口 80 和端口 443,我需要以管理员身份启动 VS2010,我对此不以为然。

因此,我想出了这个非常简单的解决方案,满足以下条件:

  1. 我希望能够在控制器类或操作级别使用RequireHttps属性

  2. 我希望 MVC 在存在 RequireHttps 属性时使用 HTTPS,如果不存在则使用 HTTP

  3. 我不想以管理员身份运行 Visual Studio

  4. 我希望能够使用 IIS Express 分配的任何 HTTP 和 HTTPS 端口

  5. 我可以重用 IIS Express 的自签名 SSL 证书,并且我不在乎是否看到无效的 SSL 提示

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

你可以在这里找到我的解决方案/代码==> 仅在生产环境中使用 ASP.NET MVC RequireHttps

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

过期以后 2024-09-25 21:45:45

端口 44300 是连续的:00 表示它是您配置为启用 SSL 的第一个应用程序; 01 将是第二个,依此类推。

由于我还通过添加 [RequireHttps] 全局属性要求我的网站只能在 HTTPS 下工作,因此我在调试时遇到了一些问题。启动时,它会自动重定向到 https://localhost/

要解决此问题在调试网站时,我只需创建一个新的 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

仅在调试时使用此类。当部署到IIS7时,您应该使用Url重写来重定向到HTTPS。

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.

天煞孤星 2024-09-25 21:45:45

Dan 的答案是正确的,但如果您在配置 IIS Express 在标准端口上通过 http 和 https 为您的网站提供服务时仍然遇到问题,这里有一个很好的教程,可以逐步指导您:

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

就我而言,我不小心删除了 IIS Express 证书。我认为它是您第一次在 Visual Studio 中使用 SSL 时生成的(在选定项目上按 F4 获取属性窗口并选中“SSS Enabled”复选框)。本教程指导我如何创建证书并修复它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文