ASP.NET .NET 4.6 核心 - 如何使用 https

ASP.NET Core on .NET 4.6 - how to do https

我正在尝试使用 IIS Express 和经典的 .NET 框架(我需要经典的 Entity Framework)在 VS2017 下的 ASP.NET Core 2.1 中运行 https。

使用 http,应用程序运行良好。在项目属性的调试部分启用 https 会使新端点确实出现在 IIS Express 任务栏中 UI,但请求它只会让我 "The connection was reset." 而不是 "localhost refused to connect."

在该窗格中设置 https 会在启动时修改 Properties\launchConfiguration.json", which in turn influences.vs\config\applicationhost.config`。

我的虚拟主机是经典默认值:

WebHost.CreateDefaultBuilder(args)
   .UseStartup<Startup>()
   .Build();

我的 settings.json.

中没有关于 url 的信息

(我认为这无关紧要,因为在此设置中,即使对于 https 请求,Kestrel 仍在 http 中提供服务,对吗?)

如果您选中了为 HTTPS 配置复选框,它应该开箱即用。

Program.cs

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
}

Startup.cs

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });


        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

Properties -> launchsettings.json

{
  "iisSettings": {
    "windowsAuthentication": false, 
    "anonymousAuthentication": true, 
    "iisExpress": {
      "applicationUrl": "http://localhost:64202",
      "sslPort": 44395
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "WebApplication4": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

这应该就是您所需要的。如果你不能让它工作并比较,请创建一个新项目。