部署安全的分散式企業報表解決方案向來極具挑戰。從報表存取控制、資料來源保護,到使用者身分驗證與授權決策,每一環都關乎資安成敗。雖然 Windows 驗證是 SQL Server 2000 Reporting Services (RS) 的預設且最安全機制,但面對網際網路/外部網路應用、非 Windows 帳號體系、或需客製化角色授權的情境,必須透過 Reporting Services 安全性擴充介面 實作自訂驗證邏輯。本文將深入解析表單驗證 (Forms Authentication) 擴充的架構、實作細節與部署步驟,並提供完整範例代碼供參考。
RS 採開放式可擴充架構,透過 Managed Code API 開發專用/共用組件,主要擴充點包含:
| 介面/類別 | 用途 |
|---|---|
IAuthenticationExtension | 實作自訂身分驗證邏輯 (驗證帳號密碼、發行 Cookie) |
IAuthorizationExtension | 實作授權檢查 (CheckAccess),依安全描述符判斷操作權限 |
IExtension | 所有擴充的基底介面 |
AceCollection / AceStruct | 存取控制項目集合與結構,定義主體 (使用者/群組) 對項目的操作權限 |
*OperationsCollection | 目錄/資料來源/資料夾/報表/資源的操作集合定義 |
LogonUser 送至 RSIAuthenticationExtension.LogonUser 驗證自訂使用者存放區 (範例中為 SQL Server 資料表)IAuthorizationExtension.CheckAccess 檢查操作權限
圖 1 表單驗證的身分驗證與授權流程
GetUserInfo 取得使用者身分CheckAccess,傳入使用者名稱、Token、安全描述符、要求操作
圖 3 授權流程示意圖
微軟建議:若 Windows 驗證可行,優先採用;僅在上述情境才實作自訂擴充。
// 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);
}
Logon.aspx (處理 Web Service 驗證)UILogon.aspx (處理瀏覽器互動)ReportingService.LogonUserpublic 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;
}
CustomSecurity.sln,加入 Microsoft.ReportingServices.Interfaces 參考 (位於 <install>\ReportServer\bin)Microsoft.Samples.ReportingServices.CustomSecurity.dll/.pdb<install>\ReportServer\bin 與 <install>\ReportManager\binLogon.aspx → <install>\ReportServer,UILogon.aspx → <install>\ReportManager\Pages<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>
<UI>
<CustomAuthenticationUI>
<loginUrl>/Pages/UILogon.aspx</loginUrl>
<UseSSL>True</UseSSL>
</CustomAuthenticationUI>
<ReportServerUrl>https://<server>/ReportServer</ReportServerUrl>
</UI>
開發環境無 SSL 時:將 <UseSSL> 設為 False,但生產環境強制要求 SSL。
rssrvpolicy.config 新增 SecurityExtensionCodeGroup,授予 FullTrust,成員資格條件為組件 URL 路徑rsmgrpolicy.config 將 MyComputer 代碼群組權限從 Execution 提升為 FullTrust<identity impersonate="false" />
<authentication mode="Forms">
<forms loginUrl="logon.aspx" name="sqlAuthCookie" timeout="60" path="/"></forms>
</authentication>
<authorization>
<deny users="?" /> <!-- 拒絕匿名使用者 -->
</authorization>
<identity impersonate="false" />
ReportServer 與 Reports 虛擬目錄皆需啟用「匿名存取」,使用 IUSR_<computername> 帳號服務初始登入頁面。
執行 createuserstore.sql 建立 UserAccounts 資料庫與 LookupUser 預存程序。注意:腳本中的 LocalMachine 需替換為實際機器名稱;Windows 2003/IIS 6.0 應用 NT AUTHORITY\NETWORK SERVICE。
iisreset 重啟 IISRSReportServer.config 將註冊帳號填入 <AdminConfiguration>System Administrator 與 Content Manager 角色非瀏覽器用戶端需自行管理 Cookie:
LogonUser 取得 CookieReportingService Proxy 類別,覆寫 GetWebRequest/GetWebResponse 實作 CookieContainer 管理aspnet_wp.exe (IIS 5) 或 w3wp.exe (IIS 6)AuthenticationExtension.LogonUser、Authorization.CheckAccess 設中斷點Web.config、RSReportServer.config、RSWebApplication.configBUILTIN\Administrators 加入 System Administrator 角色警告:遷移至自訂安全性後,若資料庫項目持有自訂安全描述符,還原 Windows 驗證可能導致存取錯誤,生產環境建議規劃單向遷移。
Reporting Services 透過安全性擴充 API 提供極大彈性,讓企業能整合既有身分管理體系 (LDAP、自訂資料表、單一登入等)。表單驗證範例展示了完整的「登入頁 → 驗證 → Cookie → 授權」閉環實作。開發者務必審慎評估自訂安全性風險,確保 SSL 加密、輸入驗證 (防 SQL Injection)、錯誤處理與稽核記錄完善落實。