首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

WCF 安全實戰系列(一):BasicHttpBinding 全安全模式實測指南——從 None 到 Transport 與 Message 安全

     編輯:關於.NET

BasicHttpBinding 定位與預設行為

BasicHttpBinding 符合 WS-Basic Profile,採 HTTP 傳輸、Text/XML 編碼,專為與 ASMX 與舊版 Web Service 互操作設計。預設安全模式為 None,訊息明文傳輸、無驗證,這也是 WCF 綁定中唯一預設不安全的。但可透過 Transport(HTTPS/IIS)或 Message(WS-Security)啟用安全性。

測試環境與共用合約

服務端:Win2008, VS2008, .NET 3.5, IIS 7.0。客戶端:同機與 Win2003。共用合約:

[ServiceContract(Namespace = "https://chnking.com")]
public interface IGetIdentity
{
    [OperationContract]
    string Get(string ClientIdentity);
}
public class GetIdentity : IGetIdentity
{
    public string Get(string ClientIdentity)
    {
        return "服務端Identity是'" + ServiceSecurityContext.Current.PrimaryIdentity.Name +
               "'\r\n客戶端Identity是 '" + ClientIdentity + "'";
    }
}

1. 安全模式:None(預設)

自宿主程式碼設定 BasicHttpSecurityMode.None。TcpTrace 擷取顯示標準 SOAP 1.1 封包,完整相容 ASMX。

Message Certificate 測試結果

2. 安全模式:Message - 客戶端 Certificate

WS-Security 保護訊息層,需雙向憑證。關鍵差異:WS-Security 無 Token 交換機制,客戶端必須預先安裝服務端憑證(WS-Trust 則可透過協商取得)。雙方產生憑證:伺服器 CN=win2008,客戶端 CN=TestClient。客戶端程式碼需同時指定自身與服務端憑證。

3. 安全模式:Transport(HTTPS/IIS 宿主)

Transport 安全必須由 IIS 承載,利用 IIS SSL 機制。步驟:

  1. 建立 WCF Service Application,專案屬性 Web 分頁設定 Use Local IIS Web Server,URL 為 https://win2008/WcfIISHostService。
  2. web.config 設定 <security mode="Transport"><transport clientCredentialType="None" /></security>。
  3. IIS 7.0:Default Web Site 新增 HTTPS Binding (port 443),選擇 SSL 憑證(CN=win2008);應用程式 SSL Settings 設定 Require SSL。
  4. 憑證產生:makecert -sr localmachine -ss My -n CN=win2008 -sky exchange -pe -r,放入 LocalMachine\Personal。
  5. 客戶端信任:將伺服器憑證匯入 CurrentUser\Trusted Root CA。

SSL 憑證三大陷阱

  • 必須放在 LocalMachine\Personal,CurrentUser 無效。
  • 憑證 CN 必須與伺服器名稱/網域名稱完全一致,否則瀏覽器與 WCF 客戶端皆報錯誤。
  • 客戶端必須信任憑證鏈所有憑證,否則同樣失敗。

MMC 憑證管理

IIS Site Binding 新增 HTTPS

選擇 SSL 憑證

SSL Settings 設定

瀏覽器憑證不信任警告

VS 偵錯憑證錯誤

VS 專案 URL 修正為 HTTPS

VS 偵斷點檢視 ServiceSecurityContext

匿名呼叫結果

4-6. Transport 模式下的客戶端驗證:Basic / NTLM / Windows

皆透過 IIS 驗證模組實作,web.config 僅需修改 clientCredentialType:

  • Basic:使用者名稱/密碼 Base64,經 SSL 加密。客戶端設定 HttpClientCredentialType.Basic 並提供 ClientCredentials.UserName。跨機器測試同樣通過。
  • NTLM:挑戰/回應,密碼不傳輸。工作群組需雙方帳號密碼相同。clientCredentialType="Ntlm"。
  • Windows:協商 Kerberos/NTLM。網域環境自動升級 Kerberos。程式碼與 NTLM 相同,只改 clientCredentialType="Windows"。

5.4 存取權限控制(NTLM/Windows 適用)

服務方法加上 [PrincipalPermission(SecurityAction.Demand, Role="Administrators")],僅允許 Administrators 群組成員呼叫。測試證實:Administrator 通過,chnking 被拒(Access is denied)。

Basic 驗證成功

跨機器 Basic 成功

NTLM 同機 Administrator

NTLM 跨機器 chnking

Administrators 角色授權通過

非 Administrators 角色被拒

7. Transport - Certificate(IIS 7.0 客戶端憑證映射)

IIS 7.0 客戶端憑證映射極其繁瑣,無 GUI 需手工操作,實務上較少用。建議改用 Message 安全模式搭配 Certificate 驗證,彈性較高。客戶端程式碼參考:proxy.ClientCredentials.ClientCertificate.SetCertificate(...)。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved