0
votes

Hi I get the following error when try to accessing the report server url. How do I pass the windows credential to the ssrs report. For your info, the report server is configure in different server therefore it will ask for the authentication to login. Please help. Thanks

System.Net.WebException: The request failed with HTTP status 401: Unauthorized. at Microsoft.Reporting.WebForms.Internal.Soap.ReportingServices2005.Execution.RSExecutionConnection.GetSecureMethods() at Microsoft.Reporting.WebForms.Internal.Soap.ReportingServices2005.Execution.RSExecutionConnection.IsSecureMethod(String methodname) at Microsoft.Reporting.WebForms.Internal.Soap.ReportingServices2005.Execution.RSExecutionConnection.SetConnectionSSLForMethod(String methodname) at Microsoft.Reporting.WebForms.Internal.Soap.ReportingServices2005.Execution.RSExecutionConnection.ProxyMethodInvocation.Execute[TReturn](RSExecutionConnection connection, ProxyMethod1 initialMethod, ProxyMethod1 retryMethod) at Microsoft.Reporting.WebForms.Internal.Soap.ReportingServices2005.Execution.RSExecutionConnection.LoadReport(String Report, String HistoryID) at Microsoft.Reporting.WebForms.SoapReportExecutionService.LoadReport(String report, String historyId) at Microsoft.Reporting.WebForms.ServerReport.EnsureExecutionSession() at Microsoft.Reporting.WebForms.ServerReport.SetParameters(IEnumerable`1 parameters)

1

1 Answers

0
votes

You need to set

ReportViewer1.ServerReport.ReportServerCredentials = new MyReportServerConnection();

where your MyReportServerConnection implementation is something like this:

private sealed class MyReportServerConnection : IReportServerConnection2        
    {
        [System.Runtime.InteropServices.DllImport("advapi32.dll", SetLastError = true)]
        public static extern bool LogonUser(
                string lpszUsername,
                string lpszDomain,
                string lpszPassword,
                int dwLogonType,
                int dwLogonProvider,
                out IntPtr phToken);

        public WindowsIdentity ImpersonationUser
        {
            get
            {
                // Use credentials from config file

                IntPtr userToken = IntPtr.Zero;

                bool success = LogonUser(
                  "reportusername",
                  "reportuserdomain",
                  "reportuserpassword",
                  9,//LOGON_TYPE_NEW_CREDENTIALS
                  3,//LOGON32_PROVIDER_WINNT50
                  out userToken);

                if (!success)
                {
                    throw new Exception("Logon user failed");
                }

                return new WindowsIdentity(userToken);
            }
        }

        public ICredentials NetworkCredentials
        {
            get
            {
                return null;
            }
        }

        public bool GetFormsCredentials(out Cookie authCookie, out string userName, out string password, out string authority)
        {
            authCookie = null;
            userName = null;
            password = null;
            authority = null;

            // Not using form credentials
            return false;
        }

        public Uri ReportServerUrl
        {
            get
            {                   
                return new Uri("http://reportserverurl/ReportServer");
            }
        }

        public int Timeout
        {
            get
            {
                return 60000; // 60 seconds
            }
        }

        public IEnumerable<Cookie> Cookies
        {
            get
            {
                // No custom cookies
                return null;
            }
        }

        public IEnumerable<string> Headers
        {
            get
            {
                // No custom headers
                return null;
            }
        }
    }