萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 資料庫 >> SQL Server 教學

Reporting Services 表單驗證擴充實作:打造自訂安全性架構

前言:企業報表安全的挑戰與擴充之路

部署安全的分散式企業報表解決方案向來極具挑戰。從報表存取控制、資料來源保護,到使用者身分驗證與授權決策,每一環都關乎資安成敗。雖然 Windows 驗證是 SQL Server 2000 Reporting Services (RS) 的預設且最安全機制,但面對網際網路/外部網路應用、非 Windows 帳號體系、或需客製化角色授權的情境,必須透過 Reporting Services 安全性擴充介面 實作自訂驗證邏輯。本文將深入解析表單驗證 (Forms Authentication) 擴充的架構、實作細節與部署步驟,並提供完整範例代碼供參考。

Reporting Services 擴充架構總覽

RS 採開放式可擴充架構,透過 Managed Code API 開發專用/共用組件,主要擴充點包含:

  • 資料處理擴充:支援非內建資料來源 (Oracle、OLE DB 以外)
  • 傳遞擴充:報表輸出至傳真、簡訊、列印裝置等
  • 轉譯擴充:自訂匯出格式 (PDF、Excel 以外)
  • 安全性擴充:本文核心,替換預設 Windows 驗證,實作自訂身分驗證與授權邏輯

安全性擴充核心介面

介面/類別用途
IAuthenticationExtension實作自訂身分驗證邏輯 (驗證帳號密碼、發行 Cookie)
IAuthorizationExtension實作授權檢查 (CheckAccess),依安全描述符判斷操作權限
IExtension所有擴充的基底介面
AceCollection / AceStruct存取控制項目集合與結構,定義主體 (使用者/群組) 對項目的操作權限
*OperationsCollection目錄/資料來源/資料夾/報表/資源的操作集合定義

表單驗證運作流程

驗證流程 (13 步驟)

  1. 使用者瀏覽 Report Manager URL
  2. 重導向至自訂登入表單 (Logon.aspx)
  3. 使用者提交帳號密碼
  4. 透過 Web Service LogonUser 送至 RS
  5. RS 呼叫自訂 IAuthenticationExtension.LogonUser 驗證自訂使用者存放區 (範例中為 SQL Server 資料表)
  6. 驗證成功,RS 建立驗證票據 (Cookie) 並管理 Session
  7. Cookie 經 HTTP 標頭回傳瀏覽器,顯示 Report Manager 介面
  8. 後續操作請求攜帶 Cookie 於 HTTP 標頭
  9. RS 驗證 Cookie 有效性
  10. Cookie 有效則從資料庫取得安全描述符與資訊
  11. 呼叫 IAuthorizationExtension.CheckAccess 檢查操作權限
  12. 授權通過,執行操作並回應
  13. Session 期間持續使用同一 Cookie 進行 URL 存取與 Web Service 呼叫

表單驗證驗證與授權流程圖

圖 1 表單驗證的身分驗證與授權流程

授權流程 (7 步驟)

  1. 攜帶 Cookie 的請求送達 RS
  2. 驗證 Cookie
  3. 呼叫 GetUserInfo 取得使用者身分
  4. 使用者發起操作
  5. RS 呼叫 CheckAccess,傳入使用者名稱、Token、安全描述符、要求操作
  6. 自訂授權邏輯比對安全描述符中的 ACL,回傳 true/false
  7. 授權成功則執行操作並回應

授權流程圖

圖 3 授權流程示意圖

何時需要自訂安全性擴充?

  • Internet/Extranet 環境無法使用 Windows 帳號
  • 擁有自訂使用者/角色體系,需對應 RS 角色授權模型
微軟建議:若 Windows 驗證可行,優先採用;僅在上述情境才實作自訂擴充。

表單驗證範例深度解析

範例架構與先決條件

  • SQL Server 2000 Reporting Services
  • IIS 6.0 + ASP.NET 1.1
  • Visual Studio .NET 2003 (C#)
  • 深入理解 ASP.NET 安全性、表單驗證、.NET Framework、RS 基於角色的安全模型

關鍵實作細節

1. 密碼雜湊與 Salt 機制

// AuthenticationUtilities.cs
internal static bool VerifyPassword(string suppliedUserName, string suppliedPassword) {
  // 從資料庫取得鹽值與雜湊密碼
  SqlCommand cmd = new SqlCommand("LookupUser", conn);
  cmd.CommandType = CommandType.StoredProcedure;
  // ... 參數設定 ...
  string dbPasswordHash = reader.GetString(0);
  string salt = reader.GetString(1);
  reader.Close();

  // 串聯密碼與鹽值並雜湊比對
  string passwordAndSalt = String.Concat(suppliedPassword, salt);
  string hashedPasswordAndSalt = FormsAuthentication.HashPasswordForStoringInConfigFile(
      passwordAndSalt, "SHA1");
  return hashedPasswordAndSalt.Equals(dbPasswordHash);
}

2. 登入頁面 (Logon.aspx / UILogon.aspx)

  • Report Server 虛擬目錄放置 Logon.aspx (處理 Web Service 驗證)
  • Report Manager 放置 UILogon.aspx (處理瀏覽器互動)
  • 兩者皆採表單驗證,提交至 ReportingService.LogonUser

3. 授權檢查實作 (CheckAccess)

public bool CheckAccess(
    string userName, IntPtr userToken, byte[] secDesc, 
    FolderOperation requiredOperation) {
  if (userName == m_adminUserName) return true; // 管理員略過檢查
  AceCollection acl = DeserializeAcl(secDesc);
  foreach (AceStruct ace in acl) {
    if (userName == ace.PrincipalName) {
      foreach (FolderOperation op in ace.FolderOperations) {
        if (op == requiredOperation) return true;
      }
    }
  }
  return false;
}

部署與配置完整步驟

1. 編譯與安裝組件

  • 開啟 CustomSecurity.sln,加入 Microsoft.ReportingServices.Interfaces 參考 (位於 <install>\ReportServer\bin)
  • 建置方案,產生 Microsoft.Samples.ReportingServices.CustomSecurity.dll/.pdb
  • 複製至 <install>\ReportServer\bin 與 <install>\ReportManager\bin
  • 複製登入頁:Logon.aspx → <install>\ReportServer,UILogon.aspx → <install>\ReportManager\Pages

2. 修改 RSReportServer.config

<Security>
  <Extension Name="Forms" 
      Type="Microsoft.Samples.ReportingServices.CustomSecurity.Authorization, 
            Microsoft.Samples.ReportingServices.CustomSecurity">
    <Configuration>
      <AdminConfiguration>
        <UserName>admin_user</UserName>
      </AdminConfiguration>
    </Configuration>
  </Extension>
</Security>
<Authentication>
  <Extension Name="Forms" 
      Type="Microsoft.Samples.ReportingServices.CustomSecurity.AuthenticationExtension, 
            Microsoft.Samples.ReportingServices.CustomSecurity" />
</Authentication>

3. 修改 RSWebApplication.config

<UI>
  <CustomAuthenticationUI>
    <loginUrl>/Pages/UILogon.aspx</loginUrl>
    <UseSSL>True</UseSSL>
  </CustomAuthenticationUI>
  <ReportServerUrl>https://<server>/ReportServer</ReportServerUrl>
</UI>
開發環境無 SSL 時:將 <UseSSL> 設為 False,但生產環境強制要求 SSL。

4. 設定程式碼存取安全策略 (rssrvpolicy.config / rsmgrpolicy.config)

  • 在 rssrvpolicy.config 新增 SecurityExtensionCodeGroup,授予 FullTrust,成員資格條件為組件 URL 路徑
  • 在 rsmgrpolicy.config 將 MyComputer 代碼群組權限從 Execution 提升為 FullTrust
  • 生產環境建議:為組件簽署強式名稱,改用強式名稱成員資格條件

5. 修改 Web.config 啟用表單驗證

Report Server Web.config

<identity impersonate="false" />
<authentication mode="Forms">
  <forms loginUrl="logon.aspx" name="sqlAuthCookie" timeout="60" path="/"></forms>
</authentication>
<authorization>
  <deny users="?" /> <!-- 拒絕匿名使用者 -->
</authorization>

Report Manager Web.config

<identity impersonate="false" />

6. IIS 啟用匿名存取

ReportServer 與 Reports 虛擬目錄皆需啟用「匿名存取」,使用 IUSR_<computername> 帳號服務初始登入頁面。

7. 建立使用者帳號資料庫

執行 createuserstore.sql 建立 UserAccounts 資料庫與 LookupUser 預存程序。注意:腳本中的 LocalMachine 需替換為實際機器名稱;Windows 2003/IIS 6.0 應用 NT AUTHORITY\NETWORK SERVICE。

測試與驗證

  1. 執行 iisreset 重啟 IIS
  2. 開啟 Report Manager,註冊管理員帳號 (寫入 UserAccounts)
  3. 修改 RSReportServer.config 將註冊帳號填入 <AdminConfiguration>
  4. 重新登入,確認具備系統管理員權限
  5. 建議將管理員加入 RS 內建 System Administrator 與 Content Manager 角色

進階主題

Web Service 用戶端整合表單驗證

非瀏覽器用戶端需自行管理 Cookie:

  • 呼叫 LogonUser 取得 Cookie
  • 繼承 ReportingService Proxy 類別,覆寫 GetWebRequest/GetWebResponse 實作 CookieContainer 管理

偵錯技巧

  • VS.NET 2003 附加至 aspnet_wp.exe (IIS 5) 或 w3wp.exe (IIS 6)
  • 在 AuthenticationExtension.LogonUser、Authorization.CheckAccess 設中斷點
  • 登入操作觸發中斷,逐行檢視驗證/授權邏輯

移除範例還原 Windows 驗證

  1. 還原備份的 Web.config、RSReportServer.config、RSWebApplication.config
  2. IIS 關閉 ReportServer 虛擬目錄匿名存取
  3. RS 自動將 BUILTIN\Administrators 加入 System Administrator 角色
  4. 需手動重新套用 Windows 使用者的角色授權
警告:遷移至自訂安全性後,若資料庫項目持有自訂安全描述符,還原 Windows 驗證可能導致存取錯誤,生產環境建議規劃單向遷移。

結語與參考資源

Reporting Services 透過安全性擴充 API 提供極大彈性,讓企業能整合既有身分管理體系 (LDAP、自訂資料表、單一登入等)。表單驗證範例展示了完整的「登入頁 → 驗證 → Cookie → 授權」閉環實作。開發者務必審慎評估自訂安全性風險,確保 SSL 加密、輸入驗證 (防 SQL Injection)、錯誤處理與稽核記錄完善落實。

參考資源

  • MSDN: ReportingService.LogonUser Method
  • MSDN: How To: Use Forms Authentication with SQL Server 2000
  • MSDN: Forms Authentication Provider / Using Role-Based Security
  • MSDN: Microsoft.ReportingServices.Interfaces Namespace
copyright © 萬盛學電腦網 all rights reserved