在ASP.NET中訪問SQL 2005報表服務的權限設置問題

在訪問遠程報表時,遇到了權限問題。因爲Sharepoint服務器和報表服務器分別在不同機器上。在Sharepoint服務器上運行一切正常,但從其他機器訪問就報沒有權限。
又是典型的double hop。
基本思想是在報表服務器上建一個報表專用賬戶,讓Sharepoint服務器以該用戶的身份調用報表服務。

客戶端 -〉(用戶真實身份) -〉SharePoint服務器 -〉(模擬報表專用賬戶身份)-〉SQL 2005報表服務器

一、使用web service訪問的處理方法
處理比較簡單,在創建web service proxy類時,模擬模擬報表專用賬戶。
        public ReportAdapter(string serverURL,string path)
        {
            ReportSvr = new ReportingServer.ReportingService();
            ReportSvr.Credentials = new System.Net.NetworkCredential("user", "pwd", "domain");
     .....
     後面的代碼不變  
        }


二、使用ReportViewer控件的處理方法

使用ReportViewer控件時會麻煩很多。首先,我們不能直接用System.Net.NetworkCredential,必須自己實現一個接口Microsoft.Reporting.WebForms.IReportServerCredentials。
不過網上有很多現成的代碼,也不需要自己寫了。

using System;
using System.Data;
using System.Configuration;
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;

/// <summary>
/// Summary description for CustomReportCredentials
/// </summary>
[Serializable]
public class CustomReportCredentials : Microsoft.Reporting.WebForms.IReportServerCredentials
{

    // local variable for network credential.
    private string _UserName;
    private string _PassWord;
    private string _DomainName;
    public CustomReportCredentials(string UserName, string PassWord, string DomainName)
    {
        _UserName = UserName;
        _PassWord = PassWord;
        _DomainName = DomainName;
    }
    public System.Security.Principal.WindowsIdentity ImpersonationUser
    {
        get
        {
            return null; // not use ImpersonationUser
        }
    }
    public System.Net.ICredentials NetworkCredentials
    {
        get
        {

            // use NetworkCredentials
            return new System.Net.NetworkCredential(_UserName, _PassWord, _DomainName);
        }
    }
    public bool GetFormsCredentials(out System.Net.Cookie authCookie, out string user, out string password, out string authority)
    {

        // not use FormsCredentials unless you have implements a custom autentication.
        authCookie = null;
        user = password = authority = null;
        return false;
    }
}
需要注意[Serializable],因爲我的頁面設置EnableSessionState="True",所有的數據都需要可以序列化。
有了類CustomReportCredentials,下面的事情就簡單了
     protected void ButtonViewReport_Click(object sender, EventArgs e)
    {
        DateTime StartDate = System.Convert.ToDateTime(TextBoxStartDate.Value);
        DateTime EndDate = System.Convert.ToDateTime(TextBoxEndDate.Value);
       
        ReportParameter[] Parameters = new ReportParameter[2];
        Parameters[0] = new ReportParameter("startdate", StartDate.ToShortDateString());
        Parameters[1] = new ReportParameter("enddate", EndDate.ToShortDateString());
        try
        {
            ReportViewer1.ServerReport.ReportServerUrl = new Uri("http://ctc-bar:81/reportserver");
            ReportViewer1.ServerReport.ReportPath = "/BARReports/EBCdetaillist";
            ReportViewer1.ServerReport.ReportServerCredentials = new CustomReportCredentials("user", "pwd)", "domain");
            ReportViewer1.ServerReport.SetParameters(Parameters);
        }
        catch (Microsoft.Reporting.WebForms.ReportSecurityException ex)
        {
            Response.Write(ex.Message);
        }
        catch (Exception ex2)
        {
            Response.Write(ex2.Message);
        }
    }
很奇怪,在設置ReportServerCredentials前,要對 ReportViewer1.ServerReport.ReportServerUrl和ReportViewer1.ServerReport.ReportPath賦值。但我明明已經在ReportViewer1的設計器中設了這兩個值。
如果不設置ReportServerCredentials,則不需要這樣。

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章