none
set credentials for ReportViewer?

    Question

  • This can't be rocket science. In RS 2000, I could set a credential like this:

    rs.Credentials = New System.Net.NetworkCredential(repSvcUserID, repSvcPwd, repSvcDomain)

    What is the equivalent to this in RS 2005, when calling a report via a webform reportViewer control?

    I've found some very complicated examples of creating your own class with IReportServerCredentials, but I can't seem to get it to work.  Is there some simple example available?

    Marie


     

    Thursday, March 09, 2006 12:27 AM

Answers

  • This code works for me.  Add the class and call it as follows:

        ReportViewer1.ServerReport.ReportServerCredentials = new ReportServerCredentials("username", "pwd", "domain");

        /// <summary>
        /// Local implementation of IReportServerCredentials
        /// </summary>
        public class ReportServerCredentials : IReportServerCredentials
        {
            private string _userName;
            private string _password;
            private string _domain;

            public ReportServerCredentials(string userName, string password, string domain)
            {
                _userName = userName;
                _password = password;
                _domain = domain;
            }

            public WindowsIdentity ImpersonationUser
            {
                get
                {
                    // Use default identity.
                    return null;
                }
            }

            public ICredentials NetworkCredentials
            {
                get
                {
                    // Use default identity.
                    return new NetworkCredential(_userName, _password, _domain);
                }
            }

            public bool GetFormsCredentials(out Cookie authCookie, out string user, out string password, out string authority)
            {
                // Do not use forms credentials to authenticate.
                authCookie = null;
                user = password = authority = null;
                return false;
            }
        }

    Tuesday, March 14, 2006 10:39 AM

All replies

  • Specifying credentials to the server in the webform control is a little more complicated.  The problem is that the webforms control will make server calls asynchronously with the page.  So the control either needs the credentials in ASP session or be able to get them when it executes the asynchronous iframe or retrieves report images..  To specify credentials, you have two options:

    1. Implement IReportServerCredentials on a serializable object and pass an instance of that object to ReportViewer.ServerReport.ReportServerCredentials.

    2. Implement IReportServerConnection and specify the type in the config file (http://msdn2.microsoft.com/en-us/library/ms251661.aspx).

    Your implementation of IReportServerCredentials should be nearly trivial.  You can pass null for most of the properties, and execute your one line of code for the NetworkCredentials property.

    Friday, March 10, 2006 11:10 PM
  • This code works for me.  Add the class and call it as follows:

        ReportViewer1.ServerReport.ReportServerCredentials = new ReportServerCredentials("username", "pwd", "domain");

        /// <summary>
        /// Local implementation of IReportServerCredentials
        /// </summary>
        public class ReportServerCredentials : IReportServerCredentials
        {
            private string _userName;
            private string _password;
            private string _domain;

            public ReportServerCredentials(string userName, string password, string domain)
            {
                _userName = userName;
                _password = password;
                _domain = domain;
            }

            public WindowsIdentity ImpersonationUser
            {
                get
                {
                    // Use default identity.
                    return null;
                }
            }

            public ICredentials NetworkCredentials
            {
                get
                {
                    // Use default identity.
                    return new NetworkCredential(_userName, _password, _domain);
                }
            }

            public bool GetFormsCredentials(out Cookie authCookie, out string user, out string password, out string authority)
            {
                // Do not use forms credentials to authenticate.
                authCookie = null;
                user = password = authority = null;
                return false;
            }
        }

    Tuesday, March 14, 2006 10:39 AM
  • Thanks very much Phil. This is exactly what I've been looking for.

    Marie

     

    Tuesday, March 14, 2006 3:26 PM
  • How would this translate to VB?  I am really struggling I have reports on a Report Server and am trying to build an webpage with the report viewer to run a report but I need to pass my UserName and Password onto the report server as it is in a shared hosting enviroment....I have been researching and still can't find the right answer.
    Friday, April 28, 2006 3:29 PM
  • Here is the VB Code for the class:

    Code Snippet

    Imports Microsoft.Reporting.WebForms
    Imports System.Net

    Public Class Class1
        Implements IReportServerCredentials

        Private _username As String
        Private _password As String
        Private _domain As String

        Public Sub New(ByVal userName As String, ByVal password As String, ByVal domain As String)
            _username = userName
            _password = password
            _domain = domain
        End Sub

        Public Function GetFormsCredentials(ByRef authCookie As System.Net.Cookie, ByRef userName As String, ByRef password As String, ByRef authority As String) As Boolean Implements Microsoft.Reporting.WebForms.IReportServerCredentials.GetFormsCredentials
            authCookie = Nothing
            userName = Nothing
            password = Nothing
            authority = Nothing
            Return False
        End Function

        Public ReadOnly Property ImpersonationUser() As System.Security.Principal.WindowsIdentity Implements Microsoft.Reporting.WebForms.IReportServerCredentials.ImpersonationUser
            Get
                Return Nothing
            End Get
        End Property

        Public ReadOnly Property NetworkCredentials() As System.Net.ICredentials Implements Microsoft.Reporting.WebForms.IReportServerCredentials.NetworkCredentials
            Get
                Return New NetworkCredential(_username, _password, _domain)
            End Get
        End Property

    End Class


    Thursday, June 21, 2007 9:32 PM
  •  

    Hi, Marie:

         What's the authentication mode for your application?  In my case, the above solution only works with <authentication mode= "None" />

     

    Thanks!

     

     

    Saturday, August 18, 2007 6:09 PM
  • Hi, i don't mean to but in but i think my problem relates to this closely.

     

    I've impemented the above VBcode, and actually i found it on another posting in a different forum. The code is almost identical except for the GetFormsCredentials function:

     

    Here:

    Code Snippet

    Public Function GetFormsCredentials(ByRef authCookie As System.Net.Cookie, ByRef userName As String, ByRef password As String, ByRef authority As String) As Boolean Implements Microsoft.Reporting.WebForms.IReportServerCredentials.GetFormsCredentials

    authCookie = Nothing

    userName = Nothing

    password = Nothing

    authority = Nothing

    Return False

    End Function

     

     

     

    There:

    Code Snippet

    Public Function GetFormsCredentials(ByRef authCookie As System.Net.Cookie, ByRef userName As String, ByRef password As String, ByRef authority As String) As Boolean Implements Microsoft.Reporting.WebForms.IReportServerCredentials.GetFormsCredentials

    userName = _userName

    password = _password

    authority = _domain

    Return Nothing

    End Function

     

     

    I've tried both but have resulted with the same problem which is an HTTP 404. I don't know why i'm getting this, my server report path is fine, i've checked it many times.

     

    Anyone have any advice?

    Tuesday, September 11, 2007 6:10 PM
  • thx very much this post was very usefull to me

     

    Friday, April 11, 2008 9:39 PM
  • I'm consistently getting an "The request failed with HTTP status 401: Access Denied." error.   I know for a fact that the creds are correct, however, because  I can log into the report no problem from the server itself.


    I have no idea how to proceed...
    Thursday, June 12, 2008 8:10 PM
  • Hi,
    I am also using the same ReportServerCredentials class as you guies have mention in above posts.
    I am using it successfully but I have to give system account's user name password that is what I dont want,
    meance can I avoid use of this username and password. 
    Is it possible to replace this authentication by some other where I dont have use system account's user name and password
    Thursday, June 11, 2009 6:59 AM
  • I have an article that describes the different types of authentication available with the report viewer control: http://blogs.msdn.com/brianhartman/archive/2008/11/21/custom-credentials-in-the-report-viewer.aspx
    Tuesday, June 23, 2009 4:55 AM
  • Yes m searching for the same answer... can't i user other credentials other than System account's username and password..  I have been searching for another problem related to it.. viewing report in my local report server works but  when report server and my application are in different server it doesnot work..   The request failed with HTTP status 401: Unauthorized occurs

    i need a help

    Sunday, August 12, 2012 5:18 AM
  • thanks .. I resolved problem.
    Wednesday, September 05, 2012 2:47 PM
  • Thanks for solution.
    Monday, January 27, 2014 4:15 PM