標籤

4GL (1) 人才發展 (10) 人物 (3) 太陽能 (4) 心理 (3) 心靈 (10) 文學 (31) 生活常識 (14) 光學 (1) 名句 (10) 即時通訊軟體 (2) 奇狐 (2) 爬蟲 (1) 音樂 (2) 產業 (5) 郭語錄 (3) 無聊 (3) 統計 (4) 新聞 (1) 經濟學 (1) 經營管理 (42) 解析度 (1) 遊戲 (5) 電學 (1) 網管 (10) 廣告 (1) 數學 (1) 機率 (1) 雜趣 (1) 證券 (4) 證券期貨 (1) ABAP (15) AD (1) agentflow (4) AJAX (1) Android (1) AnyChart (1) Apache (14) BASIS (4) BDL (1) C# (1) Church (1) CIE (1) CO (38) Converter (1) cron (1) CSS (23) DMS (1) DVD (1) Eclipse (1) English (1) excel (5) Exchange (4) Failover (1) Fedora (1) FI (57) File Transfer (1) Firefox (3) FM (2) fourjs (1) Genero (1) gladiatus (1) google (1) Google Maps API (2) grep (1) Grub (1) HR (2) html (23) HTS (8) IE (1) IE 8 (1) IIS (1) IMAP (3) Internet Explorer (1) java (4) JavaScript (22) jQuery (6) JSON (1) K3b (1) ldd (1) LED (3) Linux (117) Linux Mint (4) Load Balance (1) Microsoft (2) MIS (2) MM (51) MSSQL (1) MySQL (27) Network (1) NFS (1) Office (1) OpenSSL (1) Oracle (126) Outlook (3) PDF (6) Perl (60) PHP (33) PL/SQL (1) PL/SQL Developer (1) PM (3) Postfix (2) postfwd (1) PostgreSQL (1) PP (50) python (5) QM (1) Red Hat (4) Reporting Service (28) ruby (11) SAP (234) scp (1) SD (16) sed (1) Selenium (3) Selenium-WebDriver (5) shell (5) SQL (4) SQL server (8) sqlplus (1) SQuirreL SQL Client (1) SSH (2) SWOT (3) Symantec (2) T-SQL (7) Tera Term (2) tip (1) tiptop (24) Tomcat (6) Trouble Shooting (1) Tuning (5) Ubuntu (37) ufw (1) utf-8 (1) VIM (11) Virtual Machine (2) VirtualBox (1) vnc (3) Web Service (2) wget (1) Windows (19) Windows (1) WM (6) Xvfb (2) youtube (1) yum (2)

2011年3月19日 星期六

使用Report Viewer 載入 Reporting Service (外部網頁用/Role-based用)

1.
        目前於測試環境開始試著將週期性的資料擷取需求藉由Reporting Service彙整,以及設定排程,先於使用單位上班前產生快照集,並以mail寄出,以減少手動執行T-SQL的機會及時間,有效的運用系統所提供的服務。
        另外,有部份的例行性報表,亦也可以先使用SSRS開發,發行至Reporting Server後,再由web的程式使用ReportViewer來呼叫使用,而使用VS2005ReportViewer只要將其拖曳至設計畫面後,
點選『▼智慧標籤』即會出現如左圖的對話框,如果是要直接載入ReportServer上的報表檔,就以“Remore”的方式,設定下列參數:
‧選擇報表:<伺服器報表>
‧報表伺服器URLhttp://localhost/reportserver
‧報表路徑:/MyReport/Report1 
    可是,別以為這樣就可以了順利看到報表囉!因為Reporting Service預設是以Windows認證,所以需要再指定可以登入使用的帳號與密碼,但是問題來了,工具根本沒有提供可以設定帳號與密碼的屬性欄位!
    而要可以順利的看到報表,則需要藉用IReportServerCredentials來通過Reporting Service認證,以下的程式由微軟官網剪貼來的!

2.
using System;
using System.Data;
using System.Configuration;
using System.Net;
using System.Security.Principal;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Reporting.WebForms;
 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Init(object sender, EventArgs e)
    {
        // IsPostBack加上後,參數refresh就不會回到default value
        If (IsPostBack == false) {
        ReportViewer1.ServerReport.ReportServerCredentials = new MyReportServerCredentials();
        }
    }
}

3.
public sealed class MyReportServerCredentials:IReportServerCredentials
{
    public WindowsIdentity ImpersonationUser
    {
        get
        {
            // Use the default Windows user.  Credentials will be
            // provided by the NetworkCredentials property.
            return null;
        }
    }
    public ICredentials NetworkCredentials
    {
        get
        {
            // Read the user information from the Web.config file. 
            // By reading the information on demand instead of
            // storing it, the credentials will not be stored in
            // session, reducing the vulnerable surface area to the
            // Web.config file, which can be secured with an ACL.
 
            // User name
            string userName = ConfigurationManager.AppSettings["MyReportViewerUser"];
 
            if (string.IsNullOrEmpty(userName))
                throw new Exception("Missing user name from web.config file");
 
            // Password
            string password = ConfigurationManager.AppSettings["MyReportViewerPassword"];
 
            if (string.IsNullOrEmpty(password))
                throw new Exception("Missing password from web.config file");
 
            // Domain
            string domain = ConfigurationManager.AppSettings["MyReportViewerDomain"];
 
            if (string.IsNullOrEmpty(domain))
                throw new Exception("Missing domain from web.config file");
 
            return new NetworkCredential(userName, password, domain);
        }
    }
    public bool GetFormsCredentials(out Cookie authCookie,
                out string userName, out string password,
                out string authority)
    {
        authCookie = null;
        userName = null;
        password = null;
        authority = null;
 
        // Not using form credentials
        return false;
    }
}

4.
  而將上述的程式剪貼到程式後,還需要到Web.Config新增三個值,因為程式由appSettings取得帳號、密碼、網域值,設定如下:
     <appSettings>
           ……
           <add key="MyReportViewerUser" value="myuser"/>
           <add key="MyReportViewerPassword" value="mypasswd"/>
           <add key="MyReportViewerDomain" value="http://localhost"/>
     </appSettings>

5.
    Web.Config(黃色部份為手動新增)
<?xml version="1.0"?>
<!--
    注意: 除了手動編輯這個檔案以外,您也可以使用
    Web 管理工具設定您的應用程式設定值。請使用
    Visual Studio 中的 [網站] -> [ASP.NET 組態] 選項。
    如需完整的設定與註解清單,請參考
    machine.config.comments (通常位於
    \Windows\Microsoft.Net\Framework\v2.x\Config)
-->
<configuration>
     <appSettings>
           <add key="CrystalImageCleaner-AutoStart" value="true"/>
           <add key="CrystalImageCleaner-Sleep" value="60000"/>
           <add key="CrystalImageCleaner-Age" value="120000"/>
           <add key="MyReportViewerUser" value="myuser"/>
           <add key="MyReportViewerPassword" value="mypasswd"/>
           <add key="MyReportViewerDomain" value="http://localhost"/>
     </appSettings>
     <connectionStrings/>
     <system.web>
           <!--
            設定 compilation debug="true" 會將偵錯
            符號插入編譯過的頁面。因為這樣會
            影響效能,所以只有在開發期間才能將
            這個值設定為 true。
        -->
           <compilation debug="true">
                <assemblies>
                     <add assembly="CrystalDecisions.Web, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/>
                     <add assembly="CrystalDecisions.Shared, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/>
                     <add assembly="CrystalDecisions.ReportSource, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/>
                     <add assembly="CrystalDecisions.Enterprise.Framework, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/>
                     <add assembly="CrystalDecisions.Enterprise.Desktop.Report, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/>
                     <add assembly="CrystalDecisions.CrystalReports.Engine, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/>
                     <add assembly="CrystalDecisions.Enterprise.InfoStore, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/>
                     <add assembly="CrystalDecisions.ReportAppServer.ClientDoc, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
                     <add assembly="CrystalDecisions.Windows.Forms, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/></assemblies>
                <buildProviders>
                     <add extension=".rdlc" type="Microsoft.Reporting.RdlBuildProvider, Microsoft.ReportViewer.Common, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
                </buildProviders>
           </compilation>
           <!--
            <authentication> 區段可以用來設定 ASP.NET
            使用的安全性驗證模式,以識別連入的
            使用者。
        -->
           <authentication mode="Windows"/>
           <httpHandlers>
                <add path="CrystalImageHandler.aspx" verb="GET" type="CrystalDecisions.Web.CrystalImageHandler, CrystalDecisions.Web, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
                <add path="Reserved.ReportViewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" validate="false"/>
           </httpHandlers></system.web>
</configuration>

6.
綜合上述,簡單整理一下步驟:
一、當然是要在Reporting Server上先有可執行的報表檔及帳號密碼。
二、拖曳ReportViewer至設計版面,點選智慧標籤後設定三個參數。
三、ReportServerCredentials(微軟官網提供的認證程式)剪貼到程式適當的位置。
四、打開Web.Config,在<appSettings>標籤中加入三個參數值。

完成上述步驟後,可以直接在瀏覽器中檢視asp.net的程式囉!




沒有留言:

張貼留言