共用方式為


IReportServerConnection2 介面

在沒有工作階段狀態而使用 ReportViewer Web Form 控制項時,提供報表伺服器連接資訊。

命名空間:  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 提供將用來連接到報表伺服器的資訊 (此伺服器是為了表單驗證而設定)。 (繼承自 IReportServerCredentials。)

上層

備註

當 Web.config 設定中指定了 IReportServerConnection2 介面的實作時,將不會使用 ServerReport 執行個體上 ReportServerUrlTimeoutCookiesHeaders 屬性的值。將會改用 IReportServerConnection2 實作所提供的值。除了這些屬性以外,也將使用 IReportServerConnection2 實作所提供的認證資訊。

如需有關如何使用 ReportViewer 控制項來指定連接的詳細資訊,請參閱為 ReportViewer Web 伺服器控制項指定連接和認證

如需有關 ReportViewerServerConnection Web.config 設定的詳細資訊,請參閱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 伺服器控制項指定連接和認證