C Sharp Example

This example shows you how to authenticate to our servers. When you authenticate you will receive a session ticket. The session ticket must be used for all subsequent API calls. When a session ticket is no longer needed, PwsUnauthenticate releases the ticket. 

When you call PwsAuthenticate you may receive a redirect URL rather than a session ticket. This is because your Projector account is hosted on a different server. You should dynamically update your endpoint based on the response. This will make your application fault tolerant in the event you are moved to a new server. Please see the example below for more information.


C# Example for Service Reference

/// <summary>
/// Authenticate
/// </summary>
/// <remarks>
/// As a side effect, may return a new proxy in psc. Production code
/// should include protection against infinite recursion.
/// </remarks>
/// <param name="psc">The web service proxy object</param>
/// <param name="accountCode">Account code</param>
/// <param name="userName">User name</param>
/// <param name="password">Password</param>
/// <returns>Session ticket, or null if unsuccessful</returns>
private string Authenticate (ref PwsProjectorServicesClient psc, string accountCode, string userName, string password)
{
	 PwsAuthenticateRs rs = psc.PwsAuthenticate (new PwsAuthenticateRq () {
		   AccountCode = accountCode,
		   UserName = userName,
		   Password = password
	 });

         //if request fails bounce out
	 if (rs.Status != RequestStatus.Ok)
	 {
		   return null;
	 }

         //To prevent infinite recursion, only try to reconnect if RedirectUrl is different from current url            
         if (rs.RedirectUrl != null && psc.Endpoint.Address.Uri.AbsoluteUri.StartsWith(rs.RedirectUrl) )
         {
                  return null;
         }

         //If a RedirectUrl was returned then your account data is on a different server. Retry with new url.
	 if (rs.RedirectUrl != null)
	 {
		   Uri uri = new Uri(psc.Endpoint.Address.Uri.ToString());
		   string newUriText = rs.RedirectUrl + uri.LocalPath;
                   psc = new PwsProjectorServicesClient ("BasicHttpBinding_IPwsProjectorServices", newUriText);
		   return Authenticate (ref psc, accountCode, userName, password);
	 }

	 return rs.SessionTicket;
}

C# Example for Web Reference

It is recommended that you use a service reference instead of a web reference as a best practice. The example below authenticates with the server to obtain a session ticket. If the server is the incorrect one, then the correct one is returned and we attempt to reauthenticate using that url instead.

C# Example Code
/// <summary>
/// Authenticate
/// </summary>
/// <remarks>
///
/// </remarks>
/// <param name="psc">The web service proxy object</param>
/// <param name="accountCode">Account code</param>
/// <param name="userName">User name</param>
/// <param name="password">Password</param>
/// <returns>Session ticket, or null if unsuccessful</returns>

private string Authenticate(ref PwsProjectorServices psc, string accountCode, string userName, string password)
{
	//Enter your authentication information here
	PwsAuthenticateRs rs = psc.PwsAuthenticate(new PwsAuthenticateRq()
	{
		AccountCode = accountCode,
		UserName = userName,
		Password = password
	});

	//If request fails bounce out
	if (rs.Status != RequestStatus.Ok)
	{
		return null;
	}

	//To prevent infinite recursion, only try to reconnect if RedirectUrl is different from current url
	if (rs.RedirectUrl != null && psc.Url.StartsWith(rs.RedirectUrl))
	{
		return null;
	}

	//If a RedirectUrl was returned then your account data is on a different server. Retry with new url.
	if (rs.RedirectUrl != null)
	{
		Uri uri = new Uri(psc.Url);
		psc.Url = rs.RedirectUrl + uri.LocalPath;
		return Authenticate(ref psc, accountCode, userName, password);
	}

	return rs.SessionTicket;
}

Example Request XML

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pws="http://projectorpsa.com/PwsProjectorServices/" xmlns:req="http://projectorpsa.com/DataContracts/Requests/">
   <soapenv:Header/>
   <soapenv:Body>
      <pws:PwsAuthenticate>
         <pws:serviceRequest>
            <req:RequestId>12345</req:RequestId>
            <req:AccountCode>revcorp-tcs</req:AccountCode>
            <req:Password>password</req:Password>
            <req:UserName>tom@revcorp.tcs</req:UserName>
         </pws:serviceRequest>
      </pws:PwsAuthenticate>
   </soapenv:Body>
</soapenv:Envelope>

Example Response XML

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body>
      <PwsAuthenticateResponse xmlns="http://projectorpsa.com/PwsProjectorServices/">
         <PwsAuthenticateResult xmlns:a="http://projectorpsa.com/DataContracts/Responses/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <Messages xmlns="http://projectorpsa.com/CommonServices/" xmlns:b="http://projectorpsa.com/DataContracts/Shared/Common/"/>
            <ResponseId xmlns="http://projectorpsa.com/CommonServices/">12345</ResponseId>
            <Status xmlns="http://projectorpsa.com/CommonServices/">Ok</Status>
            <a:RedirectUrl i:nil="true"/>
            <a:SessionTicket>AVLdSsy+jR9ugkxLQ6hRnw==</a:SessionTicket>
         </PwsAuthenticateResult>
      </PwsAuthenticateResponse>
   </s:Body>
</s:Envelope>



Outputting XML in C#

When debugging, it's often useful to view the raw request/response XML. You can use the following code example to make your request/response XML accessible and output it to a log file or console.

using System.IO;


       //pass in a Projector request or response object and it will be converted to XML and written to the console window
        private void DumpXML(object requestOrResponseObject) {

            if (debug) { 
                var serxml = new System.Xml.Serialization.XmlSerializer(requestOrResponseObject.GetType());
                var ms = new MemoryStream();
                serxml.Serialize(ms, requestOrResponseObject);
                string xml = Encoding.UTF8.GetString(ms.ToArray());
                Console.WriteLine(xml);
                Console.WriteLine("");
            }
        }
  
      //example calls for a request and a response
      private int GetActiveClientCount(PwsProjectorServicesClient psc, string sessionticket, string query, bool inactiveFlag) {

            var gclRq = new PwsGetClientListRq()
            {
                SessionTicket = sessionticket,
                QueryString = query,
                IncludeInactiveFlag = inactiveFlag
            };

            PwsGetClientListRs gclRs = psc.PwsGetClientList(gclRq);

            DumpXML(gclRq);
            DumpXML(gclRs);

            return gclRs.Clients.Length;
        }