Microsoft Report Viewer登录失败

时间:2011-09-01 14:23:04

标签: vb.net

我正在使用Microsoft报表查看器构建一个asp.net Web应用程序。它使用集成安全性连接到SQL数据库。但是,当我第一次将报表页面发布到服务器后加载(一切正常在本地工作)时,我收到此错误:

    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中设置的集成安全性?

1 个答案:

答案 0 :(得分: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;
}
}