IReportServerCredentials 介面
允許應用程式提供用來連接 Reporting Services 報表伺服器的認證。
命名空間: Microsoft.Reporting.WebForms
組件: Microsoft.ReportViewer.WebForms (在 Microsoft.ReportViewer.WebForms.dll 中)
語法
'宣告
Public Interface IReportServerCredentials
public interface IReportServerCredentials
public interface class IReportServerCredentials
type IReportServerCredentials = interface end
public interface IReportServerCredentials
IReportServerCredentials 類型會公開下列成員。
屬性
名稱 | 描述 | |
---|---|---|
ImpersonationUser | 當 ReportViewer 控制項連接到報表伺服器時,取得或設定要模擬之使用者的 WindowsIdentity。 | |
NetworkCredentials | 取得或設定用來驗證報表伺服器的網路認證。 |
回頁首
方法
名稱 | 描述 | |
---|---|---|
GetFormsCredentials | 提供將用來連接到報表伺服器的資訊 (此伺服器是為了表單驗證而設定)。 |
回頁首
備註
在實作 IReportServerCredentials 介面時,知道 ReportViewer 控制項會將此物件的執行個體儲存在 ASP.NET 工作階段中是很重要的事情。如果伺服器的 ASP.NET 工作階段跨處理序而儲存 (例如在 Reporting Services 中),此類別必須標記為 Serializable,好讓它可以序列化來加以儲存。
雖然它不是必要的,但也是將 IReportServerCredentials 介面實作為無狀態物件的好作法。如此可避免在序列化物件時,將認證資訊 (如使用者名稱和密碼) 儲存起來。
如需有關如何使用 ReportViewer 控制項來指定認證的詳細資訊,請參閱為 ReportViewer Web 伺服器控制項指定連接和認證。
範例
下列範例會提供標記為 Serializable 之 IReportServerCredentials 的實作,好讓它可以序列化來加以儲存。認證資訊是從 Web.config 檔案擷取而來。此實作將會針對所有用戶端要求使用相同的認證連接到報表伺服器。
在使用此範例之前,必須將三個索引鍵值組加入到應用程式 Web.config 檔案中的 appSettings 區塊:MyReportViewerUser、MyReportViewerPassword 和 MyReportViewerDomain。這些值會對應到將用來連接到報表伺服器的使用者名稱、密碼和網域。
using System;
using System.Data;
using System.Configuration;
using System.Net;
using System.Security.Principal;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Reporting.WebForms;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Init(object sender, EventArgs e)
{
ReportViewer1.ServerReport.ReportServerCredentials =
new MyReportServerCredentials();
}
}
[Serializable]
public sealed class MyReportServerCredentials :
IReportServerCredentials
{
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;
}
}
Imports System.Net
Imports System.Security.Principal
Imports Microsoft.Reporting.WebForms
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Init(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Me.Init
ReportViewer1.ServerReport.ReportServerCredentials = _
New MyReportServerCredentials()
End Sub
End Class
<Serializable()> _
Public NotInheritable Class MyReportServerCredentials
Implements IReportServerCredentials
Public ReadOnly Property ImpersonationUser() As WindowsIdentity _
Implements IReportServerCredentials.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 IReportServerCredentials.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 IReportServerCredentials.GetFormsCredentials
authCookie = Nothing
userName = Nothing
password = Nothing
authority = Nothing
'Not using form credentials
Return False
End Function
End Class