Export (0) Print
Expand All
Expand Minimize

LogonUser Method

SQL Server 2000

ReportingService.LogonUser Method

Logs on a user and authenticates a user request to the Reporting Services Web service.

Public Sub LogonUser( _
   ByVal userName As String _
   ByVal password As String _
   ByVal authority As String _
)
   Member of [Namespace].ReportingService

public void LogonUser(
   string userName,
   string password
   string authority
);
   Member of [Namespace].ReportingService
Parameters
userName
The name of the user.
password
The password of the user.
authority
Optional. The specific authority to use when authenticating a user. For example, a Windows domain. Pass a value of null (Nothing in Visual Basic) to omit this argument.
Remarks

The LogonUser method authenticates the specified user to the Report Server Web Service when custom authentication has been configured.

LogonUser must be called over SSL. The method fails if it is not called securely. Upon successful authentication, the report server Web service generates an authentication cookie and returns the cookie in the HTTPS header. The response is returned over SSL.

Example

To compile the following code example, you must reference the Reporting Services WSDL and import certain namespaces. For more information, see Compiling and Running Code Examples. The following example code can be used to enable custom security authentication for Web service clients. In the following example, a simple console application is used to retrieve credentials from the user. The ReportingService class is extended to include the necessary overridden methods for managing authentication tickets:

A VB.NET sample is not yet available for this method.

namespace MySampleApplication
{
   class Sample
   {
      public static void Main()
      {
         // instantiate a new Web service proxy
         RSClient rs = new RSClient();

         // Pass credentials from the user
         NetworkCredential creds = rs.GetCredentials();
         rs.LogonUser(creds.UserName, creds.Password, null);
                  
         try
         {
            if (rs.CheckAuthorized())
            {
               ItemTypeEnum type = rs.GetItemType("/");
               Console.WriteLine(type);
            }
         }
         
         catch (Exception)
         {
            Console.WriteLine("Exception on call to GetItemType.");
         }

         rs.SessionHeaderValue = new SessionHeader();

         // Render arguments
         byte[] result = null;
         string reportPath = "/SampleReports/Sales Order Detail";
         string format = "IMAGE";
         string historyID = null;
         string devInfo = @"<DeviceInfo><OutputFormat>GIF</OutputFormat></DeviceInfo>";
         // Use default parameter
         ParameterValue[] parameters = null;
         

         DataSourceCredentials[] credentials = null;
         string showHideToggle = null;
         string encoding;
         string mimeType;
         Warning[] warnings = null;
         ParameterValue[] reportHistoryParameters = null;
         string[] streamIDs = null;

         try
         {
            result = rs.Render(reportPath, format, historyID, devInfo, parameters, credentials, 
               showHideToggle, out encoding, out mimeType, out reportHistoryParameters, out warnings,
               out streamIDs);
         }
         catch (SoapException e)
         {
            Console.WriteLine(e.Detail.OuterXml);
         }
         // Write the contents of the report to file.
         try
         {
            FileStream stream = File.Create( @"C:\report.gif", result.Length );
            Console.WriteLine( "File created." );
            stream.Write( result, 0, result.Length );
            Console.WriteLine( "Result written to the file." );
            stream.Close();
         }
         catch ( Exception e )
         {
            Console.WriteLine( e.Message );
         }
      }
   }
   // Class used to implement and extend the Reporting Services proxy
   // class. This extension enables cookie management.
   public class RSClient : ReportingService
   {
      public bool m_needLogon = false;
      private string m_authCookieName;
      private Cookie m_authCookie;

      public RSClient()
      {
         // Set the server URL
         base.Url = "http://localhost/reportserver/reportservice.asmx";
         // Set default credentials to integrated.
         Credentials = System.Net.CredentialCache.DefaultCredentials;
      }

      /// <summary>
      /// Gets the type of the item on the report server. Use the 
      /// new modifier to hide the base implementation.
      /// </summary>
      public new ItemTypeEnum GetItemType(string item)
      {
         ItemTypeEnum type = ItemTypeEnum.Unknown;
         try
         {
            type = base.GetItemType(item);
         }

         catch(SoapException)
         {
            return ItemTypeEnum.Unknown;
         }

         return type;
      }

      /// <summary>
      /// Get whether the given credentials can connect to the report server.
      /// Returns false if not authorized. Other errors throw an exception.
      /// </summary>
      public bool CheckAuthorized()
      {
         try
         {
            GetItemType("/");
         }
         catch (WebException e)
         {
            if (! (e.Response is HttpWebResponse) ||
               ((HttpWebResponse)e.Response).StatusCode != HttpStatusCode.Unauthorized)
            {
               throw;
            }
            return false;
         }
         catch(InvalidOperationException)
         {
            // This condition could be caused by a redirect to a forms logon page
            Console.WriteLine("InvalidOperationException");
            if (m_needLogon)
            {
               NetworkCredential creds = Credentials as NetworkCredential;
               if (creds != null && creds.UserName != null)
               {
                  try
                  {
                     base.CookieContainer = new CookieContainer();
                     base.LogonUser(creds.UserName, creds.Password, null);
                     return true;
                  }
                  catch (Exception)
                  {
                     return false;
                  }
               }
            }
            else
            {
               throw;
            }
         }
         return true;
      }
      /// <summary>
      /// Enables users to enter credentials from the command prompt.
      /// </summary>
      public NetworkCredential GetCredentials()
      {
         Console.WriteLine("Please enter your credentials.");
         Console.Write("Username: ");
         string username = Console.ReadLine();
         Console.Write("Password: ");
         string password = Console.ReadLine();
         return new NetworkCredential(username, password);
      }

      protected override WebRequest GetWebRequest(Uri uri)
      {
         HttpWebRequest request;
         request = (HttpWebRequest)HttpWebRequest.Create(uri);
         request.Credentials = base.Credentials;
         request.CookieContainer = new CookieContainer();
         if (m_authCookie != null)
         {
            request.CookieContainer.Add(m_authCookie);
         }
         return request;
      }

      protected override WebResponse GetWebResponse(WebRequest request)
      {
         WebResponse response = base.GetWebResponse(request);
         string cookieName = response.Headers["RSAuthenticationHeader"];
         if (cookieName != null)
         {
            m_authCookieName = cookieName;
            HttpWebResponse webResponse = (HttpWebResponse)response;
            Cookie authCookie = webResponse.Cookies[cookieName];
            // save it away 
            m_authCookie = authCookie;
         }
         // need to call logon
         if (response.Headers["RSNotAuthenticated"] != null)
         {
            m_needLogon = true;
         }
         return response;
      }
   }
}
See Also

ReportingService Class

Reporting Services Web Service Library

Show:
© 2015 Microsoft