2010-04-14 94 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