Software Architecture-System.DirectoryServices-enable/disable,unlock,expire account funtions via userAccountControl-Property of DirectoryEntry

April 18, 2011

System.DirectoryServices is primary namespace which provide access to Active Directory.
DirectoryEntry provide manipulation of object like user in active directory.

Click here to download – working code with enable/disable,unlock,expire account funtions. You need to download the file and rename to poc-usercontrolaccount.zip ,
then extract the files.

A function to enable a user account would like as:

        /// <summary>
        ///  To enable the account
        /// </summary>
        /// <param name="userDn">user DN under discussion</param>
      static  public void Enable(string userDn)
        {
            try
            {
                DirectoryEntry user = new DirectoryEntry(userDn);
                int val = (int)user.Properties["userAccountControl"].Value;
                user.Properties["userAccountControl"].Value = val & ~0x2;
                user.CommitChanges();
                user.Close();
                Console.WriteLine("Enabled.....:" + userDn);
            }
            catch (System.DirectoryServices.DirectoryServicesCOMException E)
            {
                Console.WriteLine("Error:Enabled.." + userDn + ":" + E.Message.ToString());
            }
        }

A function to disable a user account would be like as:

/// <summary>
      /// To Disable the account
      /// </summary>
      /// <param name="userDn">user DN under discussion</param>
        static public void Disable(string userDn)
        {
            try
            {
                DirectoryEntry user = new DirectoryEntry(userDn);
                int val = (int)user.Properties["userAccountControl"].Value;
                user.Properties["userAccountControl"].Value = val | 0x2;
                user.CommitChanges();
                user.Close();
                Console.WriteLine("Disabled.....:" + userDn);
            }
            catch (System.DirectoryServices.DirectoryServicesCOMException E)
            {
                Console.WriteLine("Error:Disabled..." + userDn + ":" + E.Message.ToString());
            }
        }

A function to unlock as locked account because of wrong password attempts would be as:

/// <summary>
      /// to unlock the account
      /// </summary>
        /// <param name="userDn">user DN under discussion</param>
        static public void Unlock(string userDn)
        {
            try
            {
                DirectoryEntry uEntry = new DirectoryEntry(userDn);
                uEntry.Properties["LockOutTime"].Value = 0; 
                uEntry.CommitChanges(); 
                uEntry.Close();
                Console.WriteLine("Unlocked.....:" + userDn);
            }
            catch (System.DirectoryServices.DirectoryServicesCOMException E)
            {
                Console.WriteLine("Error:Unlocked..." + userDn + ":" + E.Message.ToString());
            }
        }

A function to set expiry date for a user account would be as:

      /// <summary>
      /// set expiry for user account
      /// </summary>
        /// <param name="userDn">user DN under discussion</param>
      /// <param name="DExpire">Expiry date to set</param>
      static public void Expire(string userDn , string DExpire)
      {
          try
          {
              DateTime pDateTime = Convert.ToDateTime( DExpire);
              DirectoryEntry uEntry = new DirectoryEntry(userDn);              
              uEntry.InvokeSet(
                    "AccountExpirationDate",
                        new object[] { pDateTime });              
              uEntry.CommitChanges();
              uEntry.Close();
              Console.WriteLine("Expire..... UserDN:" + userDn + " Date:" + pDateTime.ToString());
          }
          catch (System.DirectoryServices.DirectoryServicesCOMException E)
          {
              Console.WriteLine("Error:Expire..." + userDn + ":" + E.Message.ToString());
          }
      }

References:
http://support.microsoft.com/kb/305144
http://msdn.microsoft.com/en-us/library/ms680832(v=vs.85).aspx

Advertisements

Software Architect Path-Key to success is continuous hard/smart work

April 17, 2011

While googling about “Software Architect Path”, I landed on Oracle page titled On the Software Architect Trail ,here Bob Rhubart – manager of the architect community on Oracle Technology Network- explains the diferent path that can be followed while exploring career as Software Architect. Really interesting stuff.
There, also notice link to CNN at RESEARCH the CNNMoney.com report,which refer Software Architect as most paid job(Ranked No 1) role for 2010 in USA.

So all software professionals out here, lets target to be Software Architect. Key to success is continuous hard/smart work ………..:)


Software Architecture-www.oodesign.com – Do visit it

April 10, 2011

While googling about “Liskov’s Substitution Principle”, I landed on site:
http://www.oodesign.com
There are many sites on net which explain OOD-Object Oriented Design, but above is the best because of its simplicity in words. Both a starter and experienced professional can benefit from it equally.
Do visit it.