SSRS:ASP.NET中引用报表远程认证之ReportServerCredentials属性的设置

  ReportViewer1.ServerReport.ReportServerCredentials这个属性需要一个接口实例。

ServerReport.ReportServerCredentials属性的类型为Microsoft.Reporting.WebForms.IReportServerCredentials,它可提供三种认证方式所需的证书(Credential):

 1) Form认证证书(GetFormsCredentials);

 2) 扮演认证证书(ImpersonationUser);

 3) 网络认证证书(NetworkCredentials). 

 当报表的服务器端使用网络认证时,需要实现NetworkCredentials接口,而且必须将GetFormsCredentials()接口的返回值设置为False,否则会导致论证失败.

  那么我们如何实现呢,Code如下:

View Code
[Serializable]
    public class CustomReportCredentials : IReportServerCredentials
    {
        private string _UserName;
        private string _PassWord;
        private string _DomainName;
        /// <summary>
        /// 自动从web.config中读取信息
        /// </summary>
        public CustomReportCredentials()
        {
            _UserName = ConfigHelper.Get("ReportViewerUser");
            _PassWord = ConfigHelper.Get("ReportViewerPassWord");
            _DomainName = ConfigHelper.Get("ReportViewerDomain");
        }
        /// <summary>
        /// 手动指定认证信息
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="PassWord"></param>
        /// <param name="DomainName"></param>
        public CustomReportCredentials(string UserName, string PassWord, string DomainName)
        {
            _UserName = UserName;
            _PassWord = PassWord;
            _DomainName = DomainName;
        }

        public System.Security.Principal.WindowsIdentity ImpersonationUser
        {
            get
            {
                // Use the default Windows user.  Credentials will be
                // provided by the NetworkCredentials property.
                return null;
            }
        }

        public ICredentials NetworkCredentials
        {
            get
            {
                // Read the user information from the Web.config file.  
                // By reading the information on demand instead of 
                // storing it, the credentials will not be stored in 
                // session, reducing the vulnerable surface area to the
                // Web.config file, which can be secured with an ACL.

                //1.若没有指定用户名和密码时,默认为本地认证
                if (string.IsNullOrEmpty(_UserName) || string.IsNullOrEmpty(_PassWord))
                    return null;
                else if (string.IsNullOrEmpty(_DomainName)) //2.若未指定域,则表示当前请求域
                    return new NetworkCredential(_UserName, _PassWord);
                else //3.用户+域认证
                    return new NetworkCredential(_UserName, _PassWord, _DomainName);
            }
        }

        public bool GetFormsCredentials(out System.Net.Cookie authCookie, out string user, out string password, out string authority)
        {
            authCookie = null;
            user = password = authority = null;

            // Not using form credentials
            return false;
        }
    }
调用方法如下:
ReportViewer1.ProcessingMode=ProcessingMode.Remote; 
IReportServerCredentials irsc =newCustomReportCredentials("administrator","xxxxxx","xxxx"); 
ReportViewer1.ServerReport.ReportServerCredentials= irsc; 
ReportViewer1.ServerReport.ReportServerUrl=newUri("http://192.168.0.1/ReportServer/"); 
ReportViewer1.ServerReport.ReportPath="/test/report1"; 
参考文章:


1.http://www.cnblogs.com/beiguren/archive/2010/01/18/1650709.html
2.http://stackoverflow.com/questions/671694/passing-credentials-to-sql-report-server-2008
3.http://stackoverflow.com/questions/9763282/reportviewer-control-using-reportservercredentials-networkcredentials
原文地址:https://www.cnblogs.com/greatwang/p/2648223.html