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

ASP.NET 2.0 SQL Server 安全性:驗證模式、連接字串、最小權限與密碼加密完整指南

     編輯:關於ASP.NET

SQL Server 三大驗證模式

模式別名認證方式連接字串關鍵字典型適用場景
Windows AuthenticationTrusted Authentication, Integrated Security使用 Windows 登入身分 (Kerberos/NTLM)Integrated Security=true 或 Trusted_Connection=yes內部網域環境、單一服務帳號執行所有應用
SQL Server AuthenticationSQL AuthenticationSQL Server 內建帳號/密碼User ID=...;Password=...;網際網路部署、共享主機、每應用獨立帳號隔離
Mixed Mode—同時啟用上述兩種視實際使用者而定遷移期、混合環境

ASP.NET 應用程式身分識別

  • IIS 5.x (舊):**ASPNET** 本機帳號。
  • IIS 6.0+:**Network Service** 內建帳號 (或應用程式集區自訂身分)。
  • Windows Authentication 時,SQL Server 驗證此 Windows 身分是否有資料庫權限。

連接字串最佳實務

Windows Authentication (推薦內部環境)

<connectionStrings>
  <add name="Northwind" 
       connectionString="Data Source=.;Initial Catalog=Northwind;Integrated Security=true" 
       providerName="System.Data.SqlClient" />
</connectionStrings>
  • 無密碼明文、自動使用應用程式集區身分、支援 Kerberos 委派。

SQL Authentication (推薦網際網路/共享主機)

<connectionStrings>
  <add name="Northwind" 
       connectionString="Data Source=.;Initial Catalog=NorthCity;User ID=AppUser;Password=P@ssw0rd" 
       providerName="System.Data.SqlClient" />
</connectionStrings>

風險:密碼明文儲存於 web.config。

密碼加密:aspnet_regiis.exe 保護區段

# 加密 connectionStrings 區段 (使用 DPAPI 機器級金鑰)
aspnet_regiis -pef "connectionStrings" "C:\Projects\MyWebApp"

# 解密 (需修改時)
aspnet_regiis -pdf "connectionStrings" "C:\Projects\MyWebApp"
  • 加密後 web.config 顯示 <connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">...</connectionStrings>。
  • 執行階段自動解密,程式碼 ConfigurationManager.ConnectionStrings["..."] 完全透明。
  • 生產環境建議使用 RSA 金鑰容器 匯出/匯入,支援 Web 伺服器叢集。

最小權限原則:建立專用資料庫使用者

-- SQL Authentication 建立登入與使用者
CREATE LOGIN AppUser WITH PASSWORD = 'StrongP@ssw0rd!';
CREATE USER AppUser FOR LOGIN AppUser;
-- 僅授予必要權限
GRANT SELECT, INSERT, UPDATE, DELETE ON SCHEMA::dbo TO AppUser;
DENY ALTER, DROP, EXECUTE ON SCHEMA::dbo TO AppUser; -- 明確拒絕高風險權限
-- 或使用資料庫角色
EXEC sp_addrolemember 'db_datareader', 'AppUser';
EXEC sp_addrolemember 'db_datawriter', 'AppUser';
-- 拒絕 db_ddladmin, db_owner 等高權限角色

嚴禁 使用 sa 或 db_owner 連接字串於 Web 應用程式。

Windows Authentication 進階:模擬與委派

  • 模擬:<identity impersonate="true" /> 或程式碼 WindowsIdentity.Impersonate(),以瀏覽器使用者身分存取 SQL。需啟用 Kerberos 委派、SPN 設定正確。
  • 應用程式集區身分:建立專用網域帳號,設定應用程式集區身分,SQL 授權該帳號。簡單、安全、可稽核。

查看目前驗證模式

-- T-SQL 查詢
SELECT SERVERPROPERTY('IsIntegratedSecurityOnly'); -- 1=Windows Only, 0=Mixed Mode

-- 登錄檔 (需備份)
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL.x\MSSQLServer\LoginMode
-- 1 = Windows Authentication, 2 = Mixed Mode

安全性稽核清單

  • [ ] 連接字串已加密 (aspnet_regiis -pef)。
  • [ ] 使用專用低權限 SQL 登入/Windows 帳號,非 sa/Administrator。
  • [ ] 僅授予 SELECT/INSERT/UPDATE/DELETE,拒絕 DDL/系統程序。
  • [ ] 啟用 SQL Server 稽核 (登入失敗、結構變更、敏感資料存取)。
  • [ ] 連接字串未出現在原始碼、記錄檔、錯誤訊息中。
  • [ ] 定期輪換密碼、憑證、金鑰容器。

結語

SQL Server 安全性核心在於選擇正確驗證模式、使用最小權限帳號、加密連接字串、啟用稽核。ASP.NET 2.0 提供 aspnet_regiis 內建加密機制,配合 IIS 應用程式集區身分隔離,能有效降低資料庫憑證外洩風險,符合企業級安全合規要求。

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