Google Code Prettify

顯示具有 TLS 標籤的文章。 顯示所有文章
顯示具有 TLS 標籤的文章。 顯示所有文章

ASP.NET Core 8 安裝到 IIS10 要如何設定 SSL/TLS

要將 ASP.NET Core 8 應用程序安裝到 IIS 10 並設定 SSL/TLS,請按照以下步驟進行:

1. 安裝 IIS 和必要的擴展

首先,確保已在 Windows 10 或 Windows Server 上安裝 IIS 及其必要的擴展:

  1. 打開 "控制面板" > "程序和功能" > "啟用或關閉 Windows 功能"。
  2. 確保已選中以下項目:
    • Internet Information Services
    • Web 管理工具
    • World Wide Web 服務
    • 應用開發功能(選中 ASP.NET 和其他需要的功能)

2. 安裝 ASP.NET Core Hosting Bundle

在目標機器上安裝 ASP.NET Core Hosting Bundle,這樣可以確保 IIS 可以運行 ASP.NET Core 應用程序。

下載 ASP.NET Core Hosting Bundle

3. 部署應用程序到 IIS

  1. 使用 Visual Studio 或命令行(dotnet publish)將 ASP.NET Core 應用程序發布到文件夾中。
  2. 在 IIS 中創建新的網站或應用程序:
    • 打開 IIS 管理器。
    • 右鍵單擊 "站點",選擇 "添加網站"。
    • 設置網站名稱、物理路徑和端口號。

4. 配置網站使用 HTTPS

獲取和安裝 SSL 憑證

您可以使用以下選項之一獲取 SSL 憑證:

  • 從受信任的憑證颁发机构(CA)購買 SSL 憑證。
  • 使用 Let's Encrypt 免費獲取 SSL 憑證。
  • 在測試環境中使用自簽名憑證。

將 SSL 憑證安裝到 IIS 伺服器上:

  1. 打開 "IIS 管理器"。
  2. 選擇伺服器節點,然後打開 "伺服器憑證"。
  3. 點擊右側操作窗格中的 "導入" 或 "創建自簽名憑證"(如果使用自簽名憑證)。

綁定 HTTPS

  1. 在 IIS 管理器中選擇您的網站。
  2. 在右側操作窗格中,點擊 "綁定"。
  3. 點擊 "添加",選擇 "https" 作為類型,然後選擇已安裝的 SSL 憑證。

5. 配置 ASP.NET Core 應用程序支持 HTTPS

Program.cs 中配置 Kestrel 以支持 HTTPS

Program.cs 文件中配置 Kestrel 以支持 HTTPS:


 csharp
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

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

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup();
            });
}

6. 強制使用 HTTPS

在 ASP.NET Core 應用程序中強制使用 HTTPS,可以在 Startup.cs 文件中配置:


csharp
public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews();

    // 配置強制 HTTPS
    services.AddHttpsRedirection(options =>
    {
        options.RedirectStatusCode = StatusCodes.Status308PermanentRedirect;
        options.HttpsPort = 443;
    });

    // 配置 HSTS
    services.AddHsts(options =>
    {
        options.Preload = true;
        options.IncludeSubDomains = true;
        options.MaxAge = TimeSpan.FromDays(60);
    });
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
        app.UseHsts();
    }

    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseRouting();
    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
    });
}

7. 驗證設置

啟動 IIS,訪問網站以驗證 HTTPS 是否正確配置。打開瀏覽器並導航到 https://yourdomain.com 以確保 SSL/TLS 配置正確。

這些步驟應該可以幫助您在 IIS 10 中正確設置 ASP.NET Core 8 應用程序的 SSL/TLS。



ASP.NET Core 8 出現下列錯誤, The SSL connection could not be established, see inner exception.

在 ASP.NET Core 8 中遇到 "The SSL connection could not be established, see inner exception" 錯誤通常是因為 HTTPS 設置問題或 SSL 憑證無法驗證。以下是一些解決此問題的步驟:


1. 檢查本地開發憑證
確認開發憑證已安裝
在本地開發環境中,您需要確保已安裝並信任 ASP.NET Core 開發憑證。可以使用以下命令檢查並安裝憑證:

bash
dotnet dev-certs https --check
dotnet dev-certs https --trust



2. 忽略 SSL 憑證驗證錯誤
在開發環境中,您可以選擇忽略 SSL 憑證驗證錯誤,但這不建議在生產環境中使用。您可以在配置 HttpClient 時忽略 SSL 憑證錯誤:

csharp
using System.Net.Http;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

var handler = new HttpClientHandler
{
    ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; }
};

var client = new HttpClient(handler);



3. 檢查 SSL 憑證
確保您正在使用的 SSL 憑證是有效的,並且已正確安裝。如果是自簽名憑證,請確保它已添加到受信任的根憑證颁发机构。


4. 配置 Kestrel
如果您使用 Kestrel 作為 Web 伺服器,請確保正確配置了 HTTPS:

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

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup();
            })
            .ConfigureKestrel(options =>
            {
                options.Listen(IPAddress.Any, 5001, listenOptions =>
                {
                    listenOptions.UseHttps("path/to/your/certificate.pfx", "yourpassword");
                });
            });
}



5. 檢查網絡設置
如果您在企業網絡或使用 VPN,有時會遇到 SSL/TLS 問題。請檢查您的網絡設置並嘗試在不同的網絡環境中運行應用程式。


6. 查看內部異常信息
檢查內部異常的詳細信息以獲取更多線索:

csharp
try
{
    var client = new HttpClient();
    var response = await client.GetAsync("https://yourapiendpoint");
    response.EnsureSuccessStatusCode();
}
catch (HttpRequestException ex)
{
    Console.WriteLine($"Request error: {ex.Message}");
    if (ex.InnerException != null)
    {
        Console.WriteLine($"Inner exception: {ex.InnerException.Message}");
    }
}



7. 設置 SSL 協議版本
確保您的應用程式配置了正確的 SSL/TLS 協議版本:

csharp
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;



總結
以上步驟應該能幫助您解決 SSL 連接建立失敗的問題。根據實際情況調整配置,特別是在開發和生產環境之間的 SSL 設置。如果問題依然存在,請提供更多詳細信息以便進一步診斷。