Updating Active Directory Field Using C#

In some cases you need to update one or more fields using C#, to do this follow-up the below code.

  • Make sure your application build is .Net 3.5

  • You need to add the following assemblies in your application reference
    • System.DirectoryServices.dll [C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.DirectoryServices.dll]
    • System.DirectoryServices.AccountManagement.dll [C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.5\System.DirectoryServices.AccountManagement.dll]
  • You need the following namespaces
    • using System.DirectoryServices.AccountManagement;
    • using System.DirectoryServices;

Here is the code

        public static bool UpdateFields(string Property, string sUserName, string Value)
        {
            bool result = false;
            PrincipalContext domain = new PrincipalContext(ContextType.Domain, "127.0.0.1", "OU=Users,DC=dev,DC=loc", ContextOptions.SimpleBind);
                // Search for the user in the domain
                PrincipalSearcher searcher = new PrincipalSearcher();
                UserPrincipal findUser = new UserPrincipal(domain);
                findUser.SamAccountName = sUserName;
                searcher.QueryFilter = findUser;
                UserPrincipal foundUser = (UserPrincipal)searcher.FindOne();
                if (foundUser != null)
                {
                    using (DirectoryEntry baseEntry = foundUser.GetUnderlyingObject() as DirectoryEntry)
                    {
                        using (DirectoryEntry entry = new DirectoryEntry(baseEntry.Path, baseEntry.Username, "P@ssw0rd"))
                        {
                            entry.Properties[Property].Value = Value;
                            entry.CommitChanges();
                            entry.Close();
                            result = true;
                        }
                    }
                }
                else
                {
                    return false;
                }
            return result;
        }

Running SSIS Package in 32-bit Mode

I have SSIS package reading from Oracle database

When running the package for testing I got the following error in Output window:

The requested OLE DB provider MSDAORA.1 is not registered. If the 64-bit driver is not installed, run the package in 32-bit mode. Error code: 0x00000000.

To run the package in 32-bit mode, apply the following steps:

In the Debugging >> Run64BitRuntime change it to False