ASP.NET Core 2-使用自定义域名和IISExpress开发SSL [英] ASP.NET Core 2 - develop using custom domain names and ssl using IISExpress

查看:113
本文介绍了ASP.NET Core 2-使用自定义域名和IISExpress开发SSL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够使用自定义域和ssl而不是localhost在本地进行开发.

I want to be able to develop locally using a custom domain and ssl rather than localhost.

如何在VS Solution中而不是localhost上设置自定义域+ ssl?

How can I setup a custom domain + ssl in VS Solution instead of localhost?

推荐答案

简单设置-使用服务器URL

如果要关联服务器以使用分配给服务器/Web主机的所有IP地址,则可以执行以下操作:

Simple Setup - Using Server URLs

If you want to associate your server to use all the IP addresses assigned to the server/web host then you can do this:

var host = new WebHostBuilder()
    .UseUrls("http://*:80", "http://localhost")
    .UseKestrel()
    .UseIISIntegration()
    .Build();

注意:如果不需要所有IP地址,则可以从http://*更改为特定的IP地址,例如http://111.111.111.111 .另外,端口不是必需的,但我已使用它来确保答案的完整性. 同样重要的是要注意SSL无法使用 UseUrls

Note: If you don't want all IP addresses, then you can change from http://* to a specific IP address such as http://111.111.111.111. Also, the port is not a requirement, but I have used it for completeness of the answer. It's also important to note that SSL won't work with UseUrls

您可以在Microsoft官方文档中找到有关服务器URL的大量其他详细信息

请注意,不建议通过Kestrel(即使使用SSL)在公共端点上进行托管,您应使用Nginx或IIS之类的反向代理.您可以从

Please note that hosting over a public endpoint via Kestrel (even with SSL) is not recommended and you should use a reverse proxy like Nginx or IIS. You can read more about it from the official Microsoft Docs here.

您没有提到是否使用Kestrel,但我假设您正在使用...在这种情况下,可以通过使用选项绑定套接字来轻松配置SSL证书.

You didn't mention if you were using Kestrel or not, but I will assume you are... In which case, you can configure an SSL certificate easily by binding sockets using the options.

以下是使用Listen方法使用TCP套接字的示例:

Here is an example of using TCP sockets using the 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,则Listen端点将覆盖UseUrls端点.

Note: That if you use both the Listen method and UseUrls, the Listen endpoints override the UseUrls endpoints.

您可以找到更多信息

You can find more info here at the official Microsoft Docs.

使用GUI
您可以右键单击该项目,然后单击[属性].

Using the GUI
You can right-click the project and click [Properties].

使用launchSettings.json.
您必须使用launchSettings.json进行配置,您可以在这里找到它:

Using launchSettings.json.
You have to configure this using the launchSettings.json which you can find here:

"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绑定将覆盖通过调用ListenUseUrls设置的所有绑定.有关更多信息,请参见简介到ASP.NET核心模块.


Configuring IIS Endpoints

If you use IIS, the URL bindings for IIS override any bindings that you set by calling either Listen or UseUrls. For more information, see Introduction to ASP.NET Core Module.

这篇关于ASP.NET Core 2-使用自定义域名和IISExpress开发SSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆