asp.net-mvc IIS Express 在启用 SSL 时默认为 HTTPS 端口 44300
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3690901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IIS Express defaulting to port 44300 for HTTPS when enabling SSL
提问by Dan Atkinson
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 answeredby Divya over on the IIS forums.
Divya 在 IIS 论坛上已经回答了这个问题。
采纳答案by Dan Atkinson
This question has been answeredby 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. 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
在 WebMatrix 中为网站启用 SSL 后,它默认使用端口 44300 并在后台执行所有绑定。我希望您尝试在配置文件中将此端口更改为 443。完成并保存后,您还需要修改 http.sys 中的绑定。您需要删除端口 44300 的现有条目并添加端口 443 的条目。为此,您可以使用 httpcfg (WinXp/Win2003) 或“netsh http”(WinVista/Win2K8/Win7)。以下是 netsh 的命令:
1) 获取现有条目 44300 的 appid 和 certhash(我假设您将使用 WebMatrix 默认安装的相同证书。如果您还想更改证书,请从证书存储区)
netsh http show sslcert
。在输出中搜索端口 44300 的条目并复制 certhash 和 appID。2) 删除 44300 的条目:
netsh http delete sslcert ipport=0.0.0.0:44300
3) 为端口 443 添加一个新条目,并在步骤 1 中复制 certhash 和 appID。
netsh http add sslcert ipport=0.0.0.0:443 certhash=<certhash> appid=<appid>
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
回答by Leng Keng
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:
经过一番研究,我能够使用 IIS Express 和对 Controller 类的OnAuthorization
方法(参考#1)的覆盖来解决这个问题。我也选择了 Hanselman (Ref#2) 推荐的路线。但是,由于两个原因,我对这两种解决方案并不完全满意:
- Ref#1's
OnAuthorization
only works at the action level, not at the controller class level - 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#1
OnAuthorization
仅适用于操作级别,而不适用于控制器类级别 - Ref#2 需要大量的设置(Win7 SDK for
makecert
)、netsh
命令,并且为了使用端口 80 和端口 443,我需要以管理员身份启动 VS2010,我不赞成。
So, I came up with this solution that is quite simplistic with the following conditions:
所以,我想出了这个非常简单的解决方案,满足以下条件:
I want to be able to use the
RequireHttps
attribute at Controller class or action levelI want MVC to use HTTPS when the
RequireHttps
attribute is present, and use HTTP if it is absentI do not want to have to run Visual Studio as administrator
I want to be able to use any HTTP and HTTPS ports that are assigned by IIS Express
I can reuse the self-signed SSL cert of IIS Express, and I do not care if I see the invalid SSL Prompt
我希望能够
RequireHttps
在控制器类或操作级别使用该属性我希望MVC在
RequireHttps
属性存在时使用HTTPS ,如果不存在则使用HTTP我不想以管理员身份运行 Visual Studio
我希望能够使用 IIS Express 分配的任何 HTTP 和 HTTPS 端口
我可以重用IIS Express的自签名SSL证书,我不在乎是否看到无效的SSL Prompt
=========================================
==========================================
You can find my solution/code here ==> ASP.NET MVC RequireHttps in Production Only
你可以在这里找到我的解决方案/代码 ==> ASP.NET MVC RequireHttps in Production Only
回答by Pierre-Alain Vigeant
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.
端口 44300 是连续的:00 表示它是您配置为启用 SSL 的第一个应用程序;01 将是第二个,依此类推。
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/
由于我还要求我的网站通过添加[RequireHttps]
全局属性只能在 HTTPS 中工作,因此我在调试时遇到了一些麻烦。启动时,它会自动重定向到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。
回答by Mariusz Pawelski
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. 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.
就我而言,我不小心删除了 IIS Express 证书。我认为它是您第一次在 Visual Studio 中使用 SSL 时生成的(在所选项目上按 F4 以获取属性窗口并选中“启用 SSS”复选框)。本教程指导我如何创建证书并修复它。
回答by Alex Vaghin
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
{
...
}