C# 将凭据传递到 Sql Report Server 2008

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/671694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 12:43:46  来源:igfitidea点击:

Passing Credentials to Sql Report Server 2008

c#asp.netreporting-servicesssrs-2008

提问by adopilot

I am pretty new in C# and my English is not so good - sorry in advance if I miss a point.

我是 C# 新手,我的英语不太好 - 如果我错过了一点,请提前抱歉。

I tried to build an ASP.NET web site with a ReportServicecontrol. As you might already know, SSRS 2008 does not allow anonymous login. So, I tried to pass Credentials to SSRS which will be stored inside my web page so that users will be able to see the report without logging in.

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

I found the code below and put it on my WebForm, but I'm having a problem with the report parameters.

我找到了下面的代码并将其放在我的WebForm.

  • If there are default values for the report parameters, the below code works okay.

  • But, if I try to change the value of a parameter, the whole page is
    refreshed and before I click the "View Report" button, all
    parameters are reset to default or null values.

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

  • 但是,如果我尝试更改参数的值,整个页面都会
    刷新,并且在我单击“查看报告”按钮之前,所有
    参数都将重置为默认值或空值。

Any suggestion on how to avoid refreshing the whole page, or another way to pass the login info to SSRS? Thanks a lot in advance.

关于如何避免刷新整个页面或将登录信息传递给 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;
     }
}

采纳答案by Mark Brackett

I really haven't messed with SSRS - but my ASP.NET hat tells me you may want to wrap that stuff in an if (!IsPostBack)block to keep it from running on the page refresh. My guess is that ReportViewer1.ServerReport.Refresh()pulls the default values again.

我真的没有搞乱 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();
    }
}

回答by adopilot

I made new function and picked it up in the design view on properties, events, reportViewer. (In selection INIT i)

我创建了新函数并在属性、事件、reportViewer 的设计视图中选取了它。(在选择 INIT i 中)

After that, the page works normally and I can change values for parameters.

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

Default.aspx now looks like:

Default.aspx 现在看起来像:

    </head>
      <body>
        <form id="form1" runat="server">
         <div>
            <rsweb:ReportViewer ID="ReportViewer1" runat="server" onload="Admir">
            </rsweb:ReportViewer>
         </div>
       </form>
    </body>

And Default.aspx.cs looks like this

而 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)
    {

    }

回答by S I

you can use the following code in page load event to give parameters

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

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();

I hope the this works for you

我希望这对你有用

回答by user1818042

i have found the solution for this. you have to set credentials first then you have to set parameter for reportviewer.

我已经找到了解决方案。您必须先设置凭据,然后才能为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();