Google Code Prettify

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

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。



不用IIS也能執行ASP.NET Web API

在某些情境,桌面環境執行的程式(Console、Windows Form、WPF… 等)也需要提供API管道供外界呼叫,例如: 先前提到的Word轉PDF服務、ERP UI接受外部(如Excel VBA)匯入資料... 等等。
設計API管道時有不少選擇: DDE、Anonymous Pipe/Named Pipe、Socket... 都可行。對轉行寫桌面程式的ASP.NET開發者來說,還有一個溫馨的好選擇 -- 在桌面程式專案裡寫ASP.NET Web API吧!!
是的,即使沒有IIS,ASP.NET Web API也能照跑不誤,在Windows Form、WPF可以繼續用同一招打天下,對跨界寫桌面程式的ASP.NET開發人員,實在是一大福音。
以下使用Console Application專案做個簡單示範。建好新專案後,透過NuGet Packages Manager尋找self host,可以找到"Microsoft ASP.NET Web API Self Host"套件,二話不說立刻安裝。
ASP.NET Web API Self Host由多個組件構成,但不用擔心,NuGet會自動一一下載安裝好。
安裝完成後,我們要在主程式中加幾行程式,啟動一個小小的Http Server。
第一步
要先透過HttpSelfHostConfiguratio宣告提供Web API的URL。由於向Windows註冊特定的TCP Port需要權限,有兩種做法: 以管理者身分執行Visual Studio及應用程式,或是透過 netsh http add urlacl url=http://+:port_number/ user=machine\username指令授權。依"永遠只授與足以執行的最小權限"的資安原則,用netsh授權雖然手續較麻煩,但比讓整個應用程式都具有管理者權限安全。

註:
開放授權 netsh http add urlacl url=http://+:32767/ user=machine\username

移除權限 netsh http delete urlacl url=http://+:32767/
接著,使用Routes.MapHttpRoute()指定MVC必備的路由設定,就可使用這組設定值宣告一個HttpSelfHostServer並啟動。由於會動用到網路資源,建議使用using HttpSelfHostServer的寫法,確保結束時會透過Dispose()釋放相關資源。
加上一段迴圈,直到使用者輸入exit才結束HttpSelfHostServer。在這段期間,HttpSelfHostServer便能接收HTTP請求,找到適當的Controller提供服務。
排版顯示純文字
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Http;
using System.Web.Http.SelfHost;
 
namespace SelfHostWebApi
{
    class Program
    {
        static void Main(string[] args)
        {
            //指定聆聽的URL
            var config = new HttpSelfHostConfiguration("http://localhost:32767");
 
//注意: 在Vista, Win7/8,預設需以管理者權限執行才能繫結到指定URL,否則要透過以下指令授權
//開放授權 netsh http add urlacl url=http://+:32767/ user=machine\username
//移除權限 netsh http delete urlacl url=http://+:32767/
 
            //設定路由
              config.Routes.MapHttpRoute("API", "{controller}/{action}/{id}", 
                                        new { id = RouteParameter.Optional });
            //設定Self-Host Server,由於會使用到網路資源,用using確保會Dispose()加以釋放
            using (var httpServer = new HttpSelfHostServer(config))
            {
                //OpenAsync()屬非同步呼叫,加上Wait()則等待開啟完成才往下執行
                   httpServer.OpenAsync().Wait();
                Console.WriteLine("Web API host started...");
                //輸入exit按Enter結束httpServer
                string line = null;
                do
                {
                    line = Console.ReadLine();
                }
                while (line != "exit");
                //結束連線
                   httpServer.CloseAsync().Wait();
            }
            
        }
    }
}
Console Application專案沒有Models、Controllers、Views資料夾,要如何加入Web API Controller讓人有些茫然,此時讓我們回歸ASP.NET MVC的"Convension over Configuration"(以慣例取代設定)原則: 在專案中新增一個名為BlahController的類別並繼承ApiController,Self Host自然會依著類別名稱認出它,並在有人呼叫http:// localhost:32767/Blah時派它上場。
為了測試,我宣告了一個很沒營養的Date方法傳回日期字串,標註[HttpGet]是為方便用瀏覽器輸入URL就能直接看結果(否則預設只接受POST,需要寫JavaScript才能測試)。
排版顯示純文字
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Http;
 
namespace SelfHostWebApi
{
    public class BlahController : ApiController
    {
        [HttpGet]
        public string Date()
        {
            return DateTime.Today.ToString("yyyy/MM/dd");
        }
    }
}
實際執行結果如下:
不會寫Socket、不懂Named Pipe,居然也能寫出具有API整合功能的桌面程式~ 衝著這點,讓我們一起呼喊: ASP.NET Web API 好威呀!


資料來源: http://blog.darkthread.net/post-2013-06-04-self-host-web-api.aspx