Edit Activedirectory user with C#

Asked

Viewed 107 times

1

Hello, I am trying to add the initials of the company’s employees in the initial parameter of the AD user and when trying to run it returns me the following message:

System.DirectoryServices.DirectoryServicesCOMException: 'O atributo ou valor do serviço de diretório especificado já existe. '

could help me?

// Lê o resultado da query SQL Server
SqlDataReader sdr = scm.ExecuteReader();

Classes.ActiveDirectory AD = new Classes.ActiveDirectory();

// Percorre todos os resultados da query e coloca na ListView
while (sdr.Read())
{

    if (!String.IsNullOrEmpty(sdr.GetString(3)))
    {

        // Realizando a consulta no AD
        DirectorySearcher ds = new DirectorySearcher(AD.abreConexaoAD());
        // Enviando uma Query para o AD
        //ds.Filter = "(&((&(objectCategory=Person)(objectClass=User)))(mail=" + sdr.GetString(3) + "))";
        ds.Filter = "(&((&(objectCategory=Person)(objectClass=User)))(sAMAccountName=api.teste))";
        // Retorna o resultado
        SearchResult rs = ds.FindOne();

        if (rs != null)
        {
            DirectoryEntry directoryEntry = rs.GetDirectoryEntry();
            directoryEntry.Properties["initials"].Add(sdr.GetString(0));

            //MessageBox.Show(directoryEntry.Properties["sAMAccountName"][0].ToString());

            // Salvando as alterações
            directoryEntry.CommitChanges();
            directoryEntry = null;

        }

    }

    System.Threading.Thread.Sleep(10000);

}
  • What is the definition of AD?

  • Activedirectory

  • Isn’t it because you are opening multiple connections with AD? one every while iteration?

  • Because it is @Leandroangelo, how do I close the connection? I am beginner and I must be doing something wrong or forgetting something.

1 answer

0

I made the following changes if successful.

Classes.ActiveDirectory AD = new Classes.ActiveDirectory();
// Realizando a consulta no AD
DirectoryEntry openConection = AD.abreConexaoAD();

//// Realizando a consulta no AD
//DirectorySearcher ds = new DirectorySearcher(AD.abreConexaoAD());

// Percorre todos os resultados da query e coloca na ListView
while (sdr.Read())
{

    if (!String.IsNullOrEmpty(sdr.GetString(3)))
    {

        //// Realizando a consulta no AD
        //DirectoryEntry openConection = AD.abreConexaoAD();
        DirectorySearcher ds = new DirectorySearcher(openConection);
        // Enviando uma Query para o AD
        //ds.Filter = "(&((&(objectCategory=Person)(objectClass=User)))(mail=" + sdr.GetString(3) + "))";
        ds.Filter = "(&((&(objectCategory=Person)(objectClass=User)))(sAMAccountName=api.teste))";
        // Retorna o resultado
        SearchResult rs = ds.FindOne();

        if (rs != null)
        {
            DirectoryEntry directoryEntry = rs.GetDirectoryEntry();
            directoryEntry.Properties["initials"].Add(sdr.GetString(0));

            //MessageBox.Show(directoryEntry.Properties["sAMAccountName"][0].ToString());

            // Salvando as alterações
            directoryEntry.CommitChanges();

        }


    }


}

openConection.Close();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.