将凭据传递给Sql Report Server 2008

我在C#中很新,我的英语不太好 – 如果我错过了一点,请提前抱歉。

我尝试使用ReportService控件构建一个ASP.NET网站。 您可能已经知道,SSRS 2008不允许匿名登录。 因此,我尝试将Credentials传递给SSRS,SSRS将存储在我的网页中,以便用户无需登录即可查看报告。

我找到了下面的代码并将其放在我的WebForm ,但我遇到了报告参数的问题。

  • 如果报告参数有默认值,则以下代码可以正常工作。

  • 但是,如果我尝试更改参数的值,整个页面就是
    刷新之前,我点击“查看报告”按钮,全部
    参数重置为默认值或空值。

有关如何避免刷新整个页面的任何建议,或者将登录信息传递给SSRS的其他方法? 非常感谢提前。

 using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Net; using Microsoft.Reporting.WebForms; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { ReportViewer1.Width = 800; ReportViewer1.Height = 600; ReportViewer1.ProcessingMode = ProcessingMode.Remote; IReportServerCredentials irsc =new CustomReportCredentials("administrator", "MYpassworw", "domena"); ReportViewer1.ServerReport.ReportServerCredentials = irsc; ReportViewer1.ServerReport.ReportServerUrl = new Uri("http://192.168.0.1/ReportServer/"); ReportViewer1.ServerReport.ReportPath = "/autonarudzba/listanarudzbi"; ReportViewer1.ServerReport.Refresh(); } } public class CustomReportCredentials : IReportServerCredentials { 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; } } public ICredentials NetworkCredentials { get { return new NetworkCredential(_UserName, _PassWord, _DomainName); } } public bool GetFormsCredentials(out Cookie authCookie, out string user, out string password, out string authority) { authCookie = null; user = password = authority = null; return false; } } 

我真的没有搞乱SSRS – 但是我的ASP.NET帽子告诉我你可能想把这些东西包装在if (!IsPostBack)块中以防止它在页面刷新上运行。 我的猜测是ReportViewer1.ServerReport.Refresh()再次拉出默认值。

 protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { ReportViewer1.Width = 800; ReportViewer1.Height = 600; ReportViewer1.ProcessingMode = ProcessingMode.Remote; IReportServerCredentials irsc =new CustomReportCredentials("administrator", "MYpassworw", "domena"); ReportViewer1.ServerReport.ReportServerCredentials = irsc; ReportViewer1.ServerReport.ReportServerUrl = new Uri("http://192.168.0.1/ReportServer/"); ReportViewer1.ServerReport.ReportPath = "/autonarudzba/listanarudzbi"; ReportViewer1.ServerReport.Refresh(); } } 

我创建了新function,并在属性,事件,reportViewer的设计视图中将其选中。 (在选择INIT i)

之后,页面正常工作,我可以更改参数的值。

Default.aspx现在看起来像:

    

Default.aspx.cs看起来像这样

  public void Admir(object sender, EventArgs e) { ReportViewer1.Width = 800; ReportViewer1.Height = 600; ReportViewer1.ProcessingMode = ProcessingMode.Remote; IReportServerCredentials irsc = new CustomReportCredentials("administrator", "mypass", "domena"); ReportViewer1.ServerReport.ReportServerCredentials = irsc; ReportViewer1.ServerReport.ReportServerUrl = new Uri("http://192.168.0.1/ReportServer/"); ReportViewer1.ServerReport.ReportPath = "/autonarudzba/listanarudzbi"; ReportViewer1.ServerReport.Refresh(); } protected void Page_Load(object sender, EventArgs e) { } 

您可以在页面加载事件中使用以下代码来提供参数

 ReportParameter[] reportParameterCollection = new ReportParameter[1]; //Array size describes the number of paramaters. reportParameterCollection[0] = new ReportParameter(); reportParameterCollection[0].Name = "ACCMGR"; //Give Your Parameter Name reportParameterCollection[0].Values.Add("12"); //Pass Parametrs's value here. ReportViewer1.ServerReport.SetParameters(reportParameterCollection); ReportViewer1.ServerReport.Refresh(); 

我希望这对你有用

我找到了解决方案。 您必须先设置凭据,然后必须为reportviewer设置参数。

  rvCommon.ProcessingMode = ProcessingMode.Remote; rvCommon.ServerReport.ReportServerUrl = new System.Uri("SSRSReportServerURL"); rvCommon.ServerReport.ReportPath = "/Report Parts/CustomerMainReport2" ; Microsoft.Reporting.WebForms.ReportParameter[] RptParameters = new Microsoft.Reporting.WebForms.ReportParameter[1]; rvCommon.ServerReport.ReportServerCredentials = new ReportCredentials("username", "password", ""); RptParameters[0] = new Microsoft.Reporting.WebForms.ReportParameter("CustomerId", "1"); rvCommon.ServerReport.SetParameters(RptParameters); rvCommon.ServerReport.Refresh();