在访问远程报表时,遇到了权限问题。因为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,则不需要这样。
