/// <summary>
        /// Constructs the object and sets values to default
        /// </summary>
        public PawnLDAPAccessor()
        {
            this.ldapConnection   = null;
            this.ldapServer       = string.Empty;
            this.state            = LDAPState.DISCONNECTED;
            this.errorMessage     = string.Empty;
            this.ldapLoginUsr     = string.Empty;
            this.ldapLoginPwd     = string.Empty;
            this.ldapUserSearchDN = string.Empty;
            this.ldapUserIdKey    = string.Empty;
            this.pwdPolicyData    = null;
            var    dNow     = DateTime.Now;
            var    yearStr  = dNow.Date.Year.ToString().PadLeft(4, '0');
            var    monthStr = dNow.Date.Month.ToString().PadLeft(2, '0');
            var    dayStr   = dNow.Date.Day.ToString().PadLeft(2, '0');
            var    hrStr    = dNow.Hour.ToString().PadLeft(2, '0');
            var    minStr   = dNow.Minute.ToString().PadLeft(2, '0');
            var    sb       = new StringBuilder(64);
            string curDir   = Directory.GetCurrentDirectory();

            sb.Append(curDir + @"\logs\ldap_details_");
            sb.AppendFormat("{0}_{1}_{2}-{3}_{4}.log", yearStr, monthStr, dayStr, hrStr, minStr);
            this.ldapLogger = new TempFileLogger(sb.ToString(),
                                                 DefaultLoggerHandlers.defaultLogLevelCheckHandler,
                                                 DefaultLoggerHandlers.defaultLogLevelGenerator,
                                                 DefaultLoggerHandlers.defaultLogMessageHandler,
                                                 DefaultLoggerHandlers.defaultLogMessageFormatHandler,
                                                 DefaultLoggerHandlers.defaultDateStampGenerator);
            this.ldapLogger.setLogLevel(LogLevel.DEBUG);
            this.ldapLogger.logMessage(LogLevel.INFO, this, "PAWNLDAPAccessor instance constructed");
        }
 public void Dispose()
 {
     try
     {
         this.state = LDAPState.DISCONNECTED;
         this.ldapConnection.Dispose();
         this.ldapConnection = null;
     }
     catch (Exception eX)
     {
         this.errorMessage = "Exception thrown while disposing accessor:" + eX.Message;
     }
     finally
     {
         this.ldapConnection = null;
         this.ldapLogger.Dispose();
     }
 }
 /// <summary>
 /// Disconnects the user from the LDAP server
 /// </summary>
 private void Disconnect()
 {
     try
     {
         if (ldapConnection != null && this.state == LDAPState.CONNECTED)
         {
             this.ldapConnection.Dispose();
             this.state          = LDAPState.DISCONNECTED;
             this.ldapConnection = null;
         }
     }
     catch (Exception eX)
     {
         this.ldapLogger.logMessage(LogLevel.FATAL, this, "Exception thrown when disconnecting LDAP: " + eX.Message);
     }
     finally
     {
         this.state          = LDAPState.DISCONNECTED;
         this.ldapConnection = null;
     }
 }
        /// <summary>
        /// Disconnects the user from the LDAP server
        /// </summary>
        public void Reconnect()
        {
            try
            {
                if (ldapConnection != null && this.state == LDAPState.CONNECTED)
                {
                    this.ldapConnection.Dispose();
                    this.state          = LDAPState.DISCONNECTED;
                    this.ldapConnection = null;
                }
                //Set main login pwd and user

/*                this.ldapLoginUsr = cxnUsr;
 *              this.ldapLoginPwd = cxnPwd;
 *              this.ldapServer = ldapSrv;
 *              this.ldapPort = ldapPrt;
 *              this.ldapPwdPolicyCN = ldapPwdCN;
 *              this.ldapUserSearchDN = ldapUsrDN;
 *              this.ldapUserIdKey = userKey;*/
                var ldapPortNum = Utilities.GetIntegerValue(this.ldapPort, LDAP_DEFAULT_PORT);

                //Login to the LDAP server and retrieve password policy
                try
                {
                    //Initialize LDAP connection
                    this.ldapConnection = new LdapConnection(
                        new LdapDirectoryIdentifier(this.ldapServer, ldapPortNum),
                        new NetworkCredential(this.ldapLoginUsr, this.ldapLoginPwd),
                        AuthType.Basic);
                    this.ldapConnection.Bind();
                    this.state = LDAPState.CONNECTED;

                    //Retrieve password policy
                    this.getPwdPolicy();
                }
                catch (LdapException lEx)
                {
                    this.ldapLogger.logMessage(LogLevel.FATAL, this, "Could not connect to the ldap server: {0}:{1} with {2}/{3}: {4}",
                                               this.ldapServer, this.ldapPort, this.ldapLoginUsr, this.ldapLoginPwd, lEx.Message);
                    this.Disconnect();
                }
                catch (Exception eX)
                {
                    this.ldapLogger.logMessage(LogLevel.FATAL, this, "Could not connect to the ldap server: {0}:{1} with {2}/{3}: {4}",
                                               this.ldapServer, this.ldapPort, this.ldapLoginUsr, this.ldapLoginPwd, eX.Message);
                    this.Disconnect();
                }
                finally
                {
                    if (this.state == LDAPState.DISCONNECTED)
                    {
                        this.ldapLogger.Dispose();
                    }
                }
            }
            catch (Exception eX)
            {
                this.ldapLogger.logMessage(LogLevel.FATAL, this, "Exception thrown when disconnecting LDAP: " + eX.Message);
            }
            finally
            {
                this.state          = LDAPState.DISCONNECTED;
                this.ldapConnection = null;
            }
        }
        public void InitializeConnection(
            string ldapSrv, string ldapPrt,
            string cxnUsr, string cxnPwd,
            string ldapPwdCN, string ldapUsrDN,
            string userKey)
        {
            if (this.state == LDAPState.CONNECTED)
            {
                return;
            }
            if (string.IsNullOrEmpty(cxnUsr) ||
                string.IsNullOrEmpty(cxnPwd) ||
                string.IsNullOrEmpty(ldapSrv) ||
                string.IsNullOrEmpty(ldapPrt) ||
                string.IsNullOrEmpty(ldapPwdCN) ||
                string.IsNullOrEmpty(ldapUsrDN) ||
                string.IsNullOrEmpty(userKey))
            {
                return;
            }
            //Set main login pwd and user
            this.ldapLoginUsr     = cxnUsr;
            this.ldapLoginPwd     = cxnPwd;
            this.ldapServer       = ldapSrv;
            this.ldapPort         = ldapPrt;
            this.ldapPwdPolicyCN  = ldapPwdCN;
            this.ldapUserSearchDN = ldapUsrDN;
            this.ldapUserIdKey    = userKey;

            var ldapPortNum = Utilities.GetIntegerValue(this.ldapPort, LDAP_DEFAULT_PORT);

            //Login to the LDAP server and retrieve password policy
            try
            {
                //Initialize LDAP connection
                this.ldapConnection = new LdapConnection(
                    new LdapDirectoryIdentifier(this.ldapServer, ldapPortNum),
                    new NetworkCredential(cxnUsr, cxnPwd),
                    AuthType.Basic);
                this.ldapConnection.Bind();
                this.state = LDAPState.CONNECTED;

                //Retrieve password policy
                this.getPwdPolicy();
            }
            catch (LdapException lEx)
            {
                this.ldapLogger.logMessage(LogLevel.FATAL, this, "Could not connect to the ldap server: {0}:{1} with {2}/{3}: {4}",
                                           ldapSrv, ldapPrt, cxnUsr, cxnPwd, lEx.Message);
                this.Disconnect();
            }
            catch (Exception eX)
            {
                this.ldapLogger.logMessage(LogLevel.FATAL, this, "Could not connect to the ldap server: {0}:{1} with {2}/{3}: {4}",
                                           ldapSrv, ldapPrt, cxnUsr, cxnPwd, eX.Message);
                this.Disconnect();
            }
            finally
            {
                if (this.state == LDAPState.DISCONNECTED)
                {
                    this.ldapLogger.Dispose();
                }
            }
        }