如何使用IIS Express在端口号53135中运行HTTPS

Sug*_*nya 5 c# https

我正在使用ASP.NET core 2.0.0,并使用它创建了可在HTTPS中运行的Web应用程序。要在HTTPS中运行,我在Program.cs中使用了以下代码

string directory = Directory.GetCurrentDirectory();
            string portNumber = Helper.getPortNumber(directory);
            var cert = new X509Certificate2("file1.pfx", "ccc");

            var host = new WebHostBuilder()
                .UseKestrel(cfg => cfg.UseHttps(cert))
                .UseUrls("https://localhost:53135")
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .Build();

            host.Run();
Run Code Online (Sandbox Code Playgroud)

然后将APPURL更改为https:// localhost:53135 /。如果我运行该程序,则会收到如下错误:

"An error occurred attempting to determine the process id of service.exe which is hosting your application.One or more errors occurred."
Run Code Online (Sandbox Code Playgroud)

但是,如果我将端口号44300设置为44399,则不会出现异常并且成功托管了该链接。我已经阅读了一个链接,该链接可以使用SSL运行IIS Express,端口号的范围应该在44300到44399之间。所有端口的应用程序。任何人都可以指导我该怎么做?

pca*_*987 5

为此,您需要管理员权限。

打开具有管理员权限的命令提示符,然后执行:

netsh http show sslcert
Run Code Online (Sandbox Code Playgroud)

搜索IP:Port介于44300到44399之间的块,并复制证书哈希和应用程序ID值。然后执行:

netsh http add sslcert ipport=0.0.0.0:53135 certhash=<HASH> appid="<APPLICATION_ID>"
Run Code Online (Sandbox Code Playgroud)

用第一步中复制的哈希值替换这些值。

之后,编辑.csproj以及可能的.csproj.user文件,以确保Visual Studio将使用正确的端口启动应用程序,例如:

<IISExpressSSLPort>53135</IISExpressSSLPort>
Run Code Online (Sandbox Code Playgroud)

有时,您可能还需要编辑.vs \ config \ applicationhost.config文件,查找绑定并更改其端口。将其更改为:

<binding protocol="https" bindingInformation="*:53135:localhost" />
Run Code Online (Sandbox Code Playgroud)

确保编辑正确项目的绑定。