在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,则不需要这样。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值