IReportServerConnection2 接口

提供在未启用会话状态下使用**“ReportViewer”**Web 窗体控件时的报表服务器连接信息。

命名空间:  Microsoft.Reporting.WebForms
程序集:  Microsoft.ReportViewer.WebForms(在 Microsoft.ReportViewer.WebForms.dll 中)

语法

声明
Public Interface IReportServerConnection2 _
    Inherits IReportServerConnection, IReportServerCredentials
用法
Dim instance As IReportServerConnection2
public interface IReportServerConnection2 : IReportServerConnection, 
    IReportServerCredentials
public interface class IReportServerConnection2 : IReportServerConnection, 
    IReportServerCredentials
type IReportServerConnection2 =  
    interface
        interface IReportServerConnection
        interface IReportServerCredentials
    end
public interface IReportServerConnection2 extends IReportServerConnection, IReportServerCredentials

IReportServerConnection2 类型公开以下成员。

属性

  名称 说明
公共属性 Cookies 获取要发送到报表服务器的自定义 Cookie 的集合。
公共属性 Headers 获取要发送到报表服务器的自定义标头的集合。
公共属性 ImpersonationUser 获取或设置当 ReportViewer 控件连接到报表服务器时要模拟的用户的 System.Security.Principal.WindowsIdentity。 (从 IReportServerCredentials 继承。)
公共属性 NetworkCredentials 获取或设置向报表服务器进行身份验证时使用的网络凭据。 (从 IReportServerCredentials 继承。)
公共属性 ReportServerUrl 返回报表服务器的 URL。 (从 IReportServerConnection 继承。)
公共属性 Timeout 获取等待服务器通信的毫秒数。 (从 IReportServerConnection 继承。)

页首

方法

  名称 说明
公共方法 GetFormsCredentials 提供用于连接到配置为使用 Forms 身份验证的报表服务器的信息。 (从 IReportServerCredentials 继承。)

页首

注释

在 Web.config 设置中指定 IReportServerConnection2 接口的实现时,将不使用 ServerReport 实例上的 ReportServerUrlTimeoutCookiesHeaders 属性的值。而是将使用 IReportServerConnection2 实现提供的值。除了这些属性外,还将使用 IReportServerConnection2 实现提供的凭据信息。

有关如何指定与 ReportViewer 控件的连接的更多信息,请参见为 ReportViewer Web 服务器控件指定连接和凭据

有关 Web.config 设置 ReportViewerServerConnection 的更多信息,请参见 ReportViewer 的 Web.config 设置

示例

下面的示例提供 IReportServerConnection2 接口的实现,该实现从 Web.config 文件中检索报表服务器的 URL 和凭据信息。

使用该示例前,必须将以下五个键/值对添加到应用程序的 Web.config 文件的 appSettings 块中:ReportViewerServerConnection、MyReportViewerUser、MyReportViewerPassword、MyReportViewerDomain 和 MyReportServerUrl,其中这些值对应于用于连接到报表服务器的用户名、密码和域以及报表服务器的 URL。必须将 ReportViewerServerConnection 值设置为 IReportServerConnection2 类的实现的完全限定程序集名称。

有关 Web.config 设置 ReportViewerServerConnection 的更多信息,请参见 ReportViewer 的 Web.config 设置

public sealed class MyReportServerConnection : IReportServerConnection2
{
    public WindowsIdentity ImpersonationUser
    {
        get
        {
            // Use the default Windows user.  Credentials will be
            // provided by the NetworkCredentials property.
            return null;
        }
    }

    public ICredentials NetworkCredentials
    {
        get
        {
            // Read the user information from the web.config file.  
            // By reading the information on demand instead of 
            // storing it, the credentials will not be stored in 
            // session, reducing the vulnerable surface area to the
            // web.config file, which can be secured with an ACL.

            // User name
            string userName =
                ConfigurationManager.AppSettings
                    ["MyReportViewerUser"];

            if (string.IsNullOrEmpty(userName))
                throw new Exception(
                    "Missing user name from Web.config file");

            // Password
            string password =
                ConfigurationManager.AppSettings
                    ["MyReportViewerPassword"];

            if (string.IsNullOrEmpty(password))
                throw new Exception(
                    "Missing password from Web.config file");

            // Domain
            string domain =
                ConfigurationManager.AppSettings
                    ["MyReportViewerDomain"];

            if (string.IsNullOrEmpty(domain))
                throw new Exception(
                    "Missing domain from Web.config file");

            return new NetworkCredential(userName, password, domain);
        }
    }

    public bool GetFormsCredentials(out Cookie authCookie,
                out string userName, out string password,
                out string authority)
    {
        authCookie = null;
        userName = null;
        password = null;
        authority = null;

        // Not using form credentials
        return false;
    }

    public Uri ReportServerUrl
    {
        get
        {
            string url = 
                ConfigurationManager.AppSettings[
                    "MyReportServerUrl"];

            if (string.IsNullOrEmpty(url))
                throw new Exception(
                    "Missing url from the Web.config file");

            return new Uri(url);
        }
    }

    public int Timeout
    {
        get
        {
            return 60000; // 60 seconds
        }
    }

    public IEnumerable<Cookie> Cookies
    {
        get
        {
            // No custom cookies
            return null;
        }
    }

    public IEnumerable<string> Headers
    {
        get
        {
            // No custom headers
            return null;
        }
    }
}
Public NotInheritable Class MyReportServerConnection
    Implements IReportServerConnection2

    Public ReadOnly Property ImpersonationUser() As WindowsIdentity _
            Implements IReportServerConnection2.ImpersonationUser
        Get

            'Use the default Windows user.  Credentials will be
            'provided by the NetworkCredentials property.
            Return Nothing

        End Get
    End Property

    Public ReadOnly Property NetworkCredentials() As ICredentials _
            Implements IReportServerConnection2.NetworkCredentials
        Get

            'Read the user information from the web.config file.  
            'By reading the information on demand instead of storing 
            'it, the credentials will not be stored in session, 
            'reducing the vulnerable surface area to the web.config 
            'file, which can be secured with an ACL.

            'User name
            Dim userName As String = _
                ConfigurationManager.AppSettings("MyReportViewerUser")

            If (String.IsNullOrEmpty(userName)) Then
                Throw New Exception("Missing user name from web.config file")
            End If

            'Password
            Dim password As String = _
                ConfigurationManager.AppSettings("MyReportViewerPassword")

            If (String.IsNullOrEmpty(password)) Then
                Throw New Exception("Missing password from web.config file")
            End If

            'Domain
            Dim domain As String = _
                ConfigurationManager.AppSettings("MyReportViewerDomain")

            If (String.IsNullOrEmpty(domain)) Then
                Throw New Exception("Missing domain from web.config file")
            End If

            Return New NetworkCredential(userName, password, domain)

        End Get
    End Property

    Public Function GetFormsCredentials(ByRef authCookie As Cookie, _
                                        ByRef userName As String, _
                                        ByRef password As String, _
                                        ByRef authority As String) _
                                        As Boolean _
            Implements IReportServerConnection2.GetFormsCredentials

        authCookie = Nothing
        userName = Nothing
        password = Nothing
        authority = Nothing

        'Not using form credentials
        Return False

    End Function

    Public ReadOnly Property ReportServerUrl() As Uri 
           Implements IReportServerConnection2.ReportServerUrl
        Get
            Dim url As String = ConfigurationManager.AppSettings("MyReportViewerUrl")
            If (String.IsNullOrEmpty(url)) Then
                Throw New Exception("Missing url from the web.config file")
            End If

            Return New Uri(url)

        End Get
    End Property

    Public ReadOnly Property Timeout() As Integer Implements IReportServerConnection2.Timeout
        Get
            Return 60000 '60 seconds
        End Get
    End Property

    Public ReadOnly Property Cookies() As IEnumerable(Of Cookie) Implements IReportServerConnection2.Cookies
        Get
            Return Nothing
        End Get
    End Property

    Public ReadOnly Property Headers() As IEnumerable(Of String) Implements IReportServerConnection2.Headers
        Get
            Return Nothing
        End Get
    End Property

End Class

请参阅

参考

Microsoft.Reporting.WebForms 命名空间

IReportServerCredentials

其他资源

ReportViewer 的 Web.config 设置

为 ReportViewer Web 服务器控件指定连接和凭据