2012-05-30 5 views
0

J'essaie d'ajouter un utilisateur au répertoire actif via un script C#. J'ai trouvé ce script sur internet (je ne l'ai pas fait moi-même). Le problème est que j'obtiens cette erreur lorsque j'essaie d'ajouter un utilisateur:Active Directory Création d'un utilisateur

L'attribut de service d'annuaire spécifié ou la valeur n'existe pas.

Ceci est le code que j'ai en ce moment:

private void buttonCreateUser_Click(object sender, EventArgs e) 
{ 
    CreateADSUser(textboxUsername.Text, textboxPassword.Text); 
} 

public string CreateADSUser(string username, string password) 
{ 
    String RootDSE; 
    try 
    { 
     DirectorySearcher DSESearcher = new DirectorySearcher(); 
     RootDSE = DSESearcher.SearchRoot.Path; 

     RootDSE = RootDSE.Insert(7, "CN=Users,"); 

     DirectoryEntry myDE = new DirectoryEntry(RootDSE); 
     DirectoryEntries myEntries = myDE.Children; 

     DirectoryEntry myDirectoryEntry = myEntries.Add("CN=" + username, "user"); 
     myDirectoryEntry.Properties["userPrincipalName"].Value = username; 
     myDirectoryEntry.Properties["name"].Value = username; 
     myDirectoryEntry.Properties["Password"].Value = password; 
     myDirectoryEntry.Properties["samAccountName"].Value = username; 
     myDirectoryEntry.Properties["FullName"].Value = username; 
     myDirectoryEntry.Properties["AccountDisabled"].Value = 0; 
     myDirectoryEntry.Properties["PasswordRequired"].Value = 1; 

     // Permanent Password? 
     myDirectoryEntry.Properties["permpass"].Value = 1; 
     myDirectoryEntry.CommitChanges(); 

     DSESearcher.Dispose(); 
     myDirectoryEntry.Dispose(); 

     textboxReports.Text = "Worked!"; 
     return "Worked!"; 
    } 
    catch (Exception ex) 
    { 
     textboxReports.Text = ex.Message; 
     return ex.Message; 
    } 
} 
+0

S'il vous plaît ne préfixe pas vos titres avec "C#" et tel. C'est ce que les tags sont pour. –

+0

Ok. Ne se reproduira pas, je ne suis pas bon à faire des titres :(. –

+0

Voir http://meta.stackexchange.com/questions/10647/how-do-i-write-a-good-title –

Répondre

1

de Nevermind, j'ai la solution!

C'est ce qu'il ressemble à ce moment:

using (var pc = new PrincipalContext(ContextType.Domain)) 
      { 
       using (var up = new UserPrincipal(pc)) 
       { 
        up.SamAccountName = textboxUsername.Text; // Username 
        up.EmailAddress = textboxEmail.Text; // Email 
        up.SetPassword(textboxPassword.Text); // Password 
        up.Enabled = true; 
        up.ExpirePasswordNow(); 
        up.Save(); 
       } 
      } 
+0

Cela fonctionnera aussi. –

1

La question est ici qu'aucune de ces propriétés existent réellement:

myDirectoryEntry.Properties["Password"].Value = password; 
myDirectoryEntry.Properties["FullName"].Value = username; 
myDirectoryEntry.Properties["AccountDisabled"].Value = 0; 
myDirectoryEntry.Properties["PasswordRequired"].Value = 1; 
myDirectoryEntry.Properties["permpass"].Value = 1; 

Celui-ci n'est pas celui que vous écrivez à:

myDirectoryEntry.Properties["name"].Value = username; 

pour (de haut en bas), voici les noms des attributs réels:

  • Mot de passe - unicodePwd
  • FullName - displayName
  • AccountDisabled - userAccountControl
  • PasswordRequired - userAccountControl (en fait, vous définissez l'inverse - que si un mot de passe n'est pas nécessaire)
  • permPass - unicodePwd (non sûr que l'objectif était avec celui-ci)
+0

Ooh, je ne savais pas ça, merci! Où puis-je trouver les noms des propriétés btw? –

+0

Commencez ici - http://msdn.microsoft.com/fr-fr/library/windows/desktop/ms675085(v=vs.85).aspx. Accédez à des classes (par exemple, utilisateur), puis ouvrez cette page pour voir les attributs sur le type spécifique d'objet. Vous pouvez également consulter les informations sur chaque attribut à partir de là. –

0

Par System.DirectoryServices.AccountManageme nt ..

   PrincipalContext ouContex = new PrincipalContext(ContextType.Domain, "TestDomain.local",   "OU=TestOU,DC=TestDomain,DC=local"); 

     for (int i = 0; i < 3; i++) 
     { 
      try 
      { 
       UserPrincipal up = new UserPrincipal(ouContex); 
       up.SamAccountName = "TestUser" + i; 
       up.SetPassword("password"); 
       up.Enabled = true; 
       up.ExpirePasswordNow(); 
       up.Save(); 
      } 
      catch (Exception ex) 
      { 

      } 
     } 

Par System.DirectoryServices

To use this namespace you need to add reference System.DirectoryServices.dll 

     DirectoryEntry ouEntry = new DirectoryEntry("LDAP://OU=TestOU,DC=TestDomain,DC=local"); 

     for (int i = 3; i < 6; i++) 
     { 
      try 
      { 
       DirectoryEntry childEntry = ouEntry.Children.Add("CN=TestUser" + i, "user"); 
       childEntry.CommitChanges(); 
       ouEntry.CommitChanges(); 
       childEntry.Invoke("SetPassword", new object[] { "password" }); 
       childEntry.CommitChanges(); 
      } 
      catch (Exception ex) 
      { 

      } 
     }