我在 ASP.net Web 应用程序中使用报表查看器控件。该应用程序以应用程序池标识 (impersonate="false") 运行,因此它有权读/写数据库。
但对于报表查看器控件,我需要模拟当前登录的用户。到目前为止,我只找到了有关将整个应用程序的模拟设置为 true 的信息,以便也模拟报表查看器。或者有关于如何创建自己的凭证并传递给查看者的信息(因此它不是当前登录的用户)。
有谁知道如何仅为报表查看器而不是整个应用程序模拟当前用户?
最佳答案
如果我理解正确,您需要实现 IReportServerCredentials,这是我过去使用的示例
using System;
using System.Configuration;
using System.Web;
using Microsoft.Reporting.WebForms;
using System.Security.Principal;
using System.Net;
[Serializable]
public sealed class ReportServerNetworkCredentials : IReportServerCredentials
{
#region IReportServerCredentials Members
private string username, password;
public ReportServerNetworkCredentials(string username, string password)
{
this.username = username;
this.password = password;
}
public bool GetFormsCredentials(out System.Net.Cookie authCookie, out string userName, out string password, out string authority)
{
//throw new NotImplementedException();
userName = password = authority = null;
// The cookie name is specified in the <forms> element in Web.config (.ASPXAUTH by default)
HttpCookie cookie = HttpContext.Current.Request.Cookies[".ASPXAUTH"];
if (cookie == null)
{
authCookie = null;
return false;
}
Cookie netCookie = new Cookie(cookie.Name, cookie.Value);
if (cookie.Domain == null)
{
netCookie.Domain = HttpContext.Current.Request.ServerVariables["SERVER_NAME"].ToUpper();
}
netCookie.Expires = cookie.Expires;
netCookie.Path = cookie.Path;
netCookie.Secure = cookie.Secure;
authCookie = netCookie;
return true;
}
public WindowsIdentity ImpersonationUser
{
get
{
return null;
}
}
public System.Net.ICredentials NetworkCredentials
{
get
{
return new System.Net.NetworkCredential(this.username, this.password);
}
}
#endregion
}
然后在带有报告查看器的 aspx 页面上输入您的凭据等
ReportViewer1.ServerReport.ReportServerCredentials = new ReportServerNetworkCredentials(username, password);
ReportViewer1.ServerReport.ReportServerUrl = new Uri("http://youreportserver/ReportServer");
ReportViewer1.ServerReport.ReportPath = reportPath;
//etc
这将允许您传递任何用户名并传递您喜欢的。这适用于 SSRS 08/SQL 08 和 id imagine 05,这是使用表单验证。
希望这对您有所帮助,并且我能正确理解您的问题。
编辑: 是的,只需从您将它们存储(成员(member)资格等)的位置检索当前用户名和密码,然后按照我的示例将其传递给。
用户名和密码必须存在于报表服务器本身中,但是,如果您确实需要为每个用户创建帐户,请考虑在应用程序本身中创建帐户时在报表服务器上为他们创建一个帐户。您可以通过 SSRS 公开的 Web 服务执行此操作,请参见此处。如果该站点已在使用中,您可以轻松地遍历所有用户并通过 SSRS 网络服务为他们创建一个帐户。
但是,您确定每个用户都需要一个报告帐户吗?如果您的原因是更多的访问权限而不是日志记录,您可以为应用程序中的每个角色创建一个帐户。
关于.net - Asp.net 报表查看器模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2636389/