ReportViewer中设置ServerReport.ReportServerCredentials属性的方法

 

当使用SSRS技术来布置报表,可能使用MS自带的ReportViewer控件来读取报表.

它分为Web和Windows两种版本;此处Web版.

 

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

 1) Form认证证书(GetFormsCredentials);

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

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

 

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

 示例代码如下:

ReportViewer1.ShowCredentialPrompts = false;

ReportViewer1.ServerReport.ReportServerCredentials = new ReportSCredentials(userName, userPwd, userAD);

ReportViewer1.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote;



public class ReportSCredentials : IReportServerCredentials

    {

        private string _UserName;

        private string _PassWord;

        private string _DomainName;

 

        public ReportSCredentials(string UserName, string PassWord, string DomainName)

        {

            _UserName = UserName;

            _PassWord = PassWord;

            _DomainName = DomainName;

        }

 

        public System.Security.Principal.WindowsIdentity ImpersonationUser

        {

            get

            {        

                return null;

            }

        }

 

        public ICredentials NetworkCredentials

        {

            get

            {

                if (string.IsNullOrEmpty(_UserName) || string.IsNullOrEmpty(_PassWord))

                {

                    return null;

                }

                else if (string.IsNullOrEmpty(_DomainName))

                {

                    return new NetworkCredential(_UserName, _PassWord);

                }

                else

                {

                    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;       

            return false;

        }

 

    }

 

原文地址:https://www.cnblogs.com/lgx5/p/6104601.html