/// <summary>
 /// Construct a new instance of LDAPAuthenticationHelper with these credentials.
 /// </summary>
 /// <param name="username">The username to identify as.</param>
 /// <param name="password">The pass to identify with.</param>
 /// <param name="key">The encryption key</param>
 /// <param name="iv">The initialization vector (in practice always "password").</param>
 /// <param name="service">The REST service exposed by the CRM instance.</param>
 public LDAPAuthenticationHelper(string username, string password, string key, string iv, string applicationName, CrmRestServer server)
 {
     this.username             = username;
     this.password             = password;
     this.key                  = key;
     this.initialisationVector = iv;
     this.applicationName      = applicationName;
     this.server               = server;
 }
        /// <summary>
        /// Construct a new instance of a UserSession. Note that all these parameters (except log)
        /// come from the settings object, and it would be much simpler to just pass that in;
        /// unfortunately, that's in the SuiteCRMAddIn assembly, and that is dependent on this, so
        /// can't be included. TODO: see if this could be refactored.
        /// </summary>
        /// <param name="URL">The URL of the rest handler to connect to.</param>
        /// <param name="Username">The username to authenticate as.</param>
        /// <param name="Password">The password to authenticate with.</param>
        /// <param name="ldapKey">The LDAP key to authenticate with.</param>
        /// <param name="log">The logger to log to.</param>
        /// <param name="timeout">The timeout for calls to the URL.</param>
        public UserSession(string URL, string Username, string Password, string ldapKey, ILogger log, int timeout)
        {
            _log            = log;
            this.restServer = new CrmRestServer(log, timeout);

            if (URL != String.Empty)
            {
                this.restServer.SuiteCRMURL = new Uri(URL);
                SuiteCRMUsername            = Username;
                SuiteCRMPassword            = Password;
                LDAPKey = ldapKey;
            }
            id = String.Empty;
        }
        /// <summary>
        /// Construct a new instance of a UserSession. Note that all these parameters (except log)
        /// come from the settings object, and it would be much simpler to just pass that in;
        /// unfortunately, that's in the SuiteCRMAddIn assembly, and that is dependent on this, so
        /// can't be included. TODO: see if this could be refactored.
        /// </summary>
        /// <param name="URL">The URL of the rest handler to connect to.</param>
        /// <param name="Username">The username to authenticate as.</param>
        /// <param name="Password">The password to authenticate with.</param>
        /// <param name="ldapKey">The LDAP key to authenticate with.</param>
        /// <param name="log">The logger to log to.</param>
        /// <param name="timeout">The timeout for calls to the URL.</param>
        public UserSession(string URL, string Username, string Password, string ldapKey, string applicationName, ILogger log, int timeout)
        {
            _log            = log;
            this.restServer = new CrmRestServer(log, timeout);

            if (URL != String.Empty)
            {
                this.restServer.SuiteCRMURL = new Uri(URL);
                this.SuiteCRMUsername       = Username;
                this.SuiteCRMPassword       = Password;
                this.LDAPKey         = string.IsNullOrWhiteSpace(ldapKey) ? null : ldapKey;
                this.ApplicationName = applicationName;
            }
            id = String.Empty;
        }