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的程式囉!




沒有留言:

張貼留言