IReportServerCredentials 接口
允许应用程序提供用于连接到 Reporting Services 报表服务器的凭据。
命名空间: Microsoft.Reporting.WebForms
程序集: Microsoft.ReportViewer.WebForms(在 Microsoft.ReportViewer.WebForms.dll 中)
语法
声明
Public Interface IReportServerCredentials
用法
Dim instance As IReportServerCredentials
public interface IReportServerCredentials
public interface class IReportServerCredentials
type IReportServerCredentials = interface end
public interface IReportServerCredentials
IReportServerCredentials 类型公开以下成员。
属性
名称 | 说明 | |
---|---|---|
ImpersonationUser | 获取或设置当 ReportViewer 控件连接到报表服务器时要模拟的用户的 System.Security.Principal.WindowsIdentity。 | |
NetworkCredentials | 获取或设置向报表服务器进行身份验证时使用的网络凭据。 |
页首
方法
名称 | 说明 | |
---|---|---|
GetFormsCredentials | 提供用于连接到配置为使用 Forms 身份验证的报表服务器的信息。 |
页首
注释
在实现 IReportServerCredentials 接口时,一定要知道 ReportViewer 控件将对象的实例存储在 ASP.NET 会话中,这一点很重要。如果服务器的 ASP.NET 会话存储在进程以外(例如存储在 Reporting Services 中),必须将类标记为 Serializable,这样便可以将类序列化以便存储。
尽管不是必需的,但将 IReportServerCredentials 接口作为无状态对象实现也是一种不错的做法。这可以阻止在序列化对象时存储用户名和密码等凭据信息。
有关如何指定用于 ReportViewer 控件的凭据的更多信息,请参见为 ReportViewer Web 服务器控件指定连接和凭据。
示例
下面的示例提供了 IReportServerCredentials 的一个实现,该实现标记为 Serializable,这样便可以将其序列化以便存储。从 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