Share via


IReportServerConnection2 Interface

Provides report server connection information when the ReportViewer Web forms control is used with no session state.

Namespace: Microsoft.Reporting.WebForms
Assembly: Microsoft.ReportViewer.WebForms (in microsoft.reportviewer.webforms.dll)

Syntax

'Declaration
Public Interface IReportServerConnection2
    Inherits IReportServerConnection, IReportServerCredentials
'Usage
Dim instance As IReportServerConnection2
public interface IReportServerConnection2 : IReportServerConnection, IReportServerCredentials
public interface class IReportServerConnection2 : IReportServerConnection, IReportServerCredentials
public interface IReportServerConnection2 extends IReportServerConnection, IReportServerCredentials
public interface IReportServerConnection2 extends IReportServerConnection, IReportServerCredentials

Remarks

When an implementation of the IReportServerConnection2 interface is specified in the Web.config settings, the values for the ReportServerUrl, Timeout, Cookies and Headers properties on the ServerReport instance will not be used. The values provided by the IReportServerConnection2 implementation will be used instead. In addition to these properties, the credential information that is provided by the IReportServerConnection2 implementation will also be used.

For more information about how to specify connections with the ReportViewer control, see Specifying Connections and Credentials for the ReportViewer Web Server Control.

For more information about the ReportViewerServerConnection Web.config setting, see Web.config Settings for ReportViewer.

Example

The following example provides an implementation of IReportServerConnection2 interface that retrieves the report server URL and credential information from the Web.config file.

Before using the example, five key/value pairs must be added to the application's Web.config file in the appSettings block: ReportViewerServerConnection, MyReportViewerUser, MyReportViewerPassword, MyReportViewerDomain, and MyReportServerUrl, where the values correspond to the user name, password, and domain for connecting to the report server, and the URL of the report server. The ReportViewerServerConnection value must be set to the fully qualified assembly name for the implementation of the IReportServerConnection2 class.

For more information about the Web.config ReportViewerServerConnection setting, see Web.config Settings for ReportViewer.

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

See Also

Reference

IReportServerConnection2 Members
Microsoft.Reporting.WebForms Namespace
IReportServerCredentials

Concepts

Web.config Settings for ReportViewer

Other Resources

Specifying Connections and Credentials for the ReportViewer Web Server Control