简单设置 - 使用服务器 URL
如果您想关联您的服务器以使用分配给服务器/网络主机的所有 IP 地址,那么您可以执行以下操作:
var host = new WebHostBuilder()
.UseUrls("http://*:80", "http://localhost")
.UseKestrel()
.UseIISIntegration()
.Build();
注意:如果您不需要所有 IP 地址,则可以从http://*
到特定的 IP 地址,例如http://111.111.111.111
。另外,端口不是必需的,但我已经使用它来保证答案的完整性。同样重要的是要注意 SSL 不能与 UseUrls
您可以在 Microsoft 官方文档中找到有关服务器 URL 的大量其他详细信息here https://learn.microsoft.com/en-us/aspnet/core/fundamentals/hosting?tabs=aspnetcore2x#server-urls.
绑定 SSL 证书(仅限 Kestrel)-- 端点配置
请注意,通过托管public不建议通过 Kestrel(即使使用 SSL)使用端点,您应该使用 Nginx 或 IIS 等反向代理。您可以从以下位置阅读更多相关信息微软官方文档在这里 https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?tabs=aspnetcore2x#when-to-use-kestrel-with-a-reverse-proxy.
您没有提到您是否使用 Kestrel,但我假设您是...在这种情况下,您可以通过使用选项绑定套接字来轻松配置 SSL 证书。
这是使用 TCP 套接字的示例Listen
method:
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseKestrel(options =>
{
options.Listen(IPAddress.Loopback, 5000);
options.Listen(IPAddress.Loopback, 5001, listenOptions =>
{
listenOptions.UseHttps("testCert.pfx", "testPassword");
});
})
.UseIISIntegration() // <-- don't forget you will need this for IIS!
.Build();
注意:如果您同时使用Listen
方法和UseUrls
, the Listen
端点覆盖UseUrls
端点。
您可以找到更多信息here https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?tabs=aspnetcore2x#endpoint-configuration在微软官方文档中。
配置 IISExpress
使用图形用户界面
您可以右键单击该项目,然后单击[属性]。
使用 launchSettings.json。
您必须使用以下命令进行配置launchSettings.json
您可以在这里找到:
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:61471/",
"sslPort": 44360
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "https://localhost:44360",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
配置 IIS 端点
如果您使用 IIS,则 IIS 的 URL 绑定会覆盖您通过调用以下任一方法设置的任何绑定:Listen
or UseUrls
。有关更多信息,请参阅ASP.NET Core 模块简介 https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/aspnet-core-module?tabs=aspnetcore2x.