Running all SSRS reports through one report user ignoring own users domain

耗尽温柔 提交于 2019-12-05 16:23:35

Windows Forms

In a windows forms project you can pass a suitable System.Net.NetworkCredential to ServerReport.ReportServerCredentials.NetworkCredentials property of ReportViewer. This way, all reports will be executed using the passed credential:

reportViewer1.ServerReport.ReportServerCredentials.NetworkCredentials =
    new System.Net.NetworkCredential("username", "password", "domain");

Web Forms

The solution for a Web Forms is different. In a Web Forms project, to pass a suitable credential to RePortViewer you need to implement IReportServerCredentials. Then you can assign the value to ServerReport.ReportServerCredentials property of ReportViewer control. This way, all reports will be executed using the passed credential.

Example

Here is a simple implementation. It's better to store username, password and domain name in app config and read them from config file:

using System;
using System.Net;
using System.Security.Principal;
using Microsoft.Reporting.WebForms;
[Serializable]
public sealed class MyReportServerCredentials : IReportServerCredentials
{
    public WindowsIdentity ImpersonationUser { get { return null; } }
    public ICredentials NetworkCredentials
    {
        get
        {
            return new NetworkCredential("username", "password", "domain");
        }
    }
    public bool GetFormsCredentials(out Cookie authCookie, out string userName,
        out string password, out string authority)
    {
        authCookie = null;
        userName = password = authority = null;
        return false;
    }
}

Then in Page_Load pass the credential this way:

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
        this.ReportViewer1.ServerReport.ReportServerCredentials = 
            new Sample.MyReportServerCredentials();
}

Note

In cases which you want to use ReportViewer with no session state you can also implement IReportServerConnection. In this case you need to add a key value in appsettings section of config file to introduce the implementation this way:

<add key="ReportViewerServerConnection" value="YourNameSpace.YourClass, YourAssemply" />

In this case, you don't need code in Page_Load and the config would be enough. For more information take a look at this great blog post by Brian Hartman.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!