.NET 1.6上的ASP.NET核心 - 如何执行https

问题描述 投票:1回答:1

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

使用http,该应用程序运行良好。在项目属性的调试部分中启用https会使新端点确实出现在IIS Express任务栏UI中,但请求它只会让我“连接已重置”。而不是“localhost拒绝连接”。

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

我的网站主机是经典的默认设置:

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

我对settings.json的网址没有任何意见。

(我不认为它是相关的,因为Kestrel仍在http中服务,即使对于此设置中的https请求,对吗?)

c# asp.net-core iis-express
1个回答
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"
      }
    }
  }
}

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

© www.soinside.com 2019 - 2024. All rights reserved.