2011-09-01 53 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