2010-04-14 95 views
3

我正在使用的ASP.net Web应用中的报表查看器控件。应用程序以应用程序池标识(impersonate =“false”)运行,因此它有权读取/写入数据库。Asp.net报告查看器模拟

但对于报表查看器控件,我需要冒充当前登录的用户。到目前为止,我只找到关于将整个应用程序的模拟设置为true的信息,以便报告查看器也可以模拟。或者有关于如何创建传递给查看器的凭证的信息(所以它不是当前登录的用户)。

有谁知道如何来冒充当前用户只对报表查看器,但不是为整个应用程序?

回答

3

如果我理解正确的话,你需要实现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,这是使用表单身份验证。

希望这有助于和我正确地理解你的问题。


编辑: 是只检索你在哪里都被他们(会员等)存储到当前用户名和密码,进入按我的例子。

的用户名和通行证必须在报表服务器本身存在但是,如果你真的需要占每个用户,当您创建的应用程序了自己的帐户考虑建立他们的报表服务器上的帐户。您可以通过SSRS公开的Web服务完成此操作,请参阅此处。如果该网站已在使用中,则可以轻松遍历所有用户,并通过SSRS Web服务为其创建帐户。

但是,你确定你需要为每个用户一个报告的帐户?如果您的理由有更多的访问权限,您可以为应用程序中的每个角色创建一个帐户。

+0

正如我写的,我想模拟当前登录的用户。我如何将这些传递给自定义凭据“新的ReportServerNetworkCredentials(loggedOnUserName?,loggedOnPassword?)” – Chris 2010-04-14 12:01:39