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

ASP.NET 2.0 資料教程 73:保護 Web.config 敏感資訊-連接字串加密、節點保護與安全模式資料庫連接建議

     編輯:關於ASP.NET

前言

Web.config 儲存連接字串、帳號密碼、加密金鑰等敏感資訊。雖然 ASP.NET 預設封鎖 .config 直接下載,但仍需防範攻擊者透過其他管道讀取。.NET 2.0 提供保護組態系統,可加密/解密指定節點。

敏感資訊常見節點

  • <connectionStrings>
  • <appSettings>
  • <identity>
  • <sessionState>

保護組態系統操作

1. 使用 aspnet_regiis.exe 加密節點

# 加密 connectionStrings
aspnet_regiis -pe "connectionStrings" -app "/MyApp" -prov "RsaProtectedConfigurationProvider"

# 加密 appSettings
aspnet_regiis -pe "appSettings" -app "/MyApp"

執行後 Web.config 該區段變為 <connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider"><EncryptedData>...</EncryptedData></connectionStrings>。

2. 程式碼動態加密/解密

Configuration config = WebConfigurationManager.OpenWebConfiguration("/MyApp");
ConnectionStringsSection section = config.ConnectionStrings;
if (!section.SectionInformation.IsProtected)
{
    section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
    config.Save();
}

3. 解密還原

aspnet_regiis -pd "connectionStrings" -app "/MyApp"
# 或程式碼 section.SectionInformation.UnprotectSection();

Microsoft 資料庫連接安全建議

  1. 連接字串必須加密。
  2. 使用 Windows 整合驗證 (Trusted_Connection=Yes),避免密碼出現在連接字串。
  3. 資料庫啟用 安全模式:僅允許特定 IP/帳戶連接、最小權限原則、稽核登入失敗。
  4. 生產環境勿使用 sa / 系統管理員帳戶連接應用程式。

結語

善用 .NET 2.0 內建保護組態系統加密敏感節點,搭配 Windows 整合驗證與資料庫層級安全設定,構建纵深防禦,守護應用程式核心機密。

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