簡體   English   中英

.NET 4.6 上的 ASP.NET Core - 如何做 https

[英]ASP.NET Core on .NET 4.6 - how to do https

我試圖讓 https 在 VS2017 下的 ASP.NET Core 2.1 中使用 IIS Express 和經典的 .NET 框架(我需要經典的實體框架)。

使用 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 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"
      }
    }
  }
}

這應該就是你所需要的。 如果您無法使其工作並進行比較,請創建一個新項目。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM