2011-09-01 52 views
0

我正在使用Microsoft報告查看器構建一個asp.net Web應用程序。它使用集成安全性連接到SQL數據庫。然而,當我第一次加載報告頁面已發佈到服務器後(一切正常本地),我得到這個錯誤:Microsoft Report Viewer登錄失敗

An error has occurred during report processing. 
Exception has been thrown by the target of an invocation. 
Login failed for user 'SERVER NAME REMOVED'. 

奇怪的是,當我點擊報表上的刷新按鈕(不是IE的刷新按鈕),它加載得很好。我知道登錄對數據庫服務器不起作用,但爲什麼報告查看器不使用我在web.config中設置的集成安全性?

回答

0

您需要實現IReportServerCredentials inteface,請參見下面的實現:

public class CustomReportCredentials : IReportServerCredentials 
    { 
     protected string _username = string.Empty; 
     protected string _password = string.Empty; 
     protected string _domainName = string.Empty; 

     public CustomReportCredentials(string userName, string password, string domainName) 
     { 
      _username = userName; 
      _password = password; 
      _domainName = domainName; 
     } 
     public bool GetFormsCredentials(out System.Net.Cookie authCookie, out string userName, out string password, out string authority) 
     { 
      authCookie = null; 
      userName = password = authority = null; 
      return false; 
     } 

     public System.Security.Principal.WindowsIdentity ImpersonationUser 
     { 
      get { return null; } 
     } 

     public System.Net.ICredentials NetworkCredentials 
     { 
      get 
      { 
       if (_username != "noadmin") 
       { 
        return new NetworkCredential(_username, _password, _domainName); 
       } 
       else 
       { 
        Uri uri = new Uri("http://tempuri.org/"); 
        ICredentials credentials = CredentialCache.DefaultCredentials; 
        NetworkCredential credential = credentials.GetCredential(uri, "Basic"); 
        return credential; 
       } 
      } 
     } 
    } 

然後你就可以報告查看證書屬性設置爲IReportServerCredentials

protected void Page_Load(object sender, EventArgs e) 
    { 
if (!Page.IsPostBack){ 
IReportServerCredentials iReportCredentials = new CustomReportCredentials("user", 
        "mypassword", "mydomain"); 
       ReportViewer1.ServerReport.ReportServerCredentials = iReportCredentials; 
} 
} 
+0

謝謝,這一定是我錯過了。有沒有辦法讓這使用集成安全,而不是我必須設置用戶名/密碼? – user842818