.NET 4.6上的ASP.NET Core-如何执行https

时间:2018-09-07 14:59:41

标签: c# asp.net-core iis-express

我试图让https在IIS VS2017和IIS Express和经典.NET框架(我需要经典Entity Framework)下的ASP.NET Core 2.1中工作。

使用http,该应用程序可以正常运行。在项目属性的调试部分中启用https,会使新终结点确实出现在IIS Express任务栏UI中,但请求它仅使我“连接已重置”。而不是 “本地主机拒绝连接。”

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

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

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

我的settings.json中的网址与我无关。

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

1 个答案:

答案 0 :(得分:1)

如果选中了“为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"
      }
    }
  }
}

这应该是您所需要的。如果无法正常工作并进行比较,请创建一个新项目。