Share via


Erstellen von Benutzern

Im folgenden Beispiel wird gezeigt, wie ein Benutzer in einer Organisationseinheit erstellt wird. Dieses Konto ist standardmäßig deaktiviert. Weitere Informationen finden Sie unter dem Thema Aktivieren und Deaktivieren von Benutzerkonten.

DirectoryEntry ent = new DirectoryEntry();
DirectoryEntry ou = ent.Children.Find("OU=Consulting");

// Use the Add method to add a user to an organizational unit.
DirectoryEntry usr = ou.Children.Add("CN=New User","user");
// Set the samAccountName, then commit changes to the directory.
usr.Properties["samAccountName"].Value = "newuser"; 
usr.CommitChanges();

Die Aufgabe kann auch in Visual Basic geschafft werden:

imports Microsoft.VisualBasic

imports System

imports System.Collections

imports System.DirectoryServices

public module MyModule

sub Main

Test()

end sub

sub Test()

try

Dim AD as new

DirectoryEntry("LDAP://Nami/CN=Users,DC=DeploymentCentric,DC=com")

AD.AuthenticationType = AuthenticationTypes.Secure

'AD.Username = "NetBiosName\UserName"

'AD.Password = "password"

Dim newUser as DirectoryEntry =

AD.Children.Add("cn=HOBOJOE", "user")

newUser.Properties("sAMAccountName").Value = "HOBOJOE"

newUser.Invoke("Put", new object() {"Description", "Test User from .NET"})

newUser.CommitChanges()

newUser.Invoke("SetPassword", new object() {"#1A" +

"password"})

Dim val as ADS_USER_FLAG_ENUM =

DirectCast(newUser.Properties("userAccountControl").Value,

ADS_USER_FLAG_ENUM)

val = val And (Not

ADS_USER_FLAG_ENUM.ADS_UF_ACCOUNTDISABLE) Or ADS_USER_FLAG_ENUM.ADS_UF_DONT_EXPIRE_PASSWD

newUser.Properties("userAccountControl").Value = val

newUser.CommitChanges()

catch ex as Exception

Console.WriteLine(ex)

finally

Console.ReadLine()

end try

end sub

<FlagsAttribute()> _

public enum ADS_GROUP_TYPE_ENUM

ADS_GROUP_TYPE_GLOBAL_GROUP = &H2

ADS_GROUP_TYPE_DOMAIN_LOCAL_GROUP = &H4

ADS_GROUP_TYPE_LOCAL_GROUP = &H4

ADS_GROUP_TYPE_UNIVERSAL_GROUP = &H8

ADS_GROUP_TYPE_SECURITY_ENABLED = &H80000000

end enum

<FlagsAttribute()> _

public enum ADS_USER_FLAG_ENUM

ADS_UF_SCRIPT = &H0001

ADS_UF_ACCOUNTDISABLE = &H0002

ADS_UF_HOMEDIR_REQUIRED = &H0008

ADS_UF_LOCKOUT = &H0010

ADS_UF_PASSWD_NOTREQD = &H0020

ADS_UF_PASSWD_CANT_CHANGE = &H0040

ADS_UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED = &H0080

ADS_UF_TEMP_DUPLICATE_ACCOUNT = &H0100

ADS_UF_NORMAL_ACCOUNT = &H0200

ADS_UF_INTERDOMAIN_TRUST_ACCOUNT = &H0800

ADS_UF_WORKSTATION_TRUST_ACCOUNT = &H1000

ADS_UF_SERVER_TRUST_ACCOUNT = &H2000

ADS_UF_DONT_EXPIRE_PASSWD = &H10000

ADS_UF_MNS_LOGON_ACCOUNT = &H20000

ADS_UF_SMARTCARD_REQUIRED = &H40000

ADS_UF_TRUSTED_FOR_DELEGATION = &H80000

ADS_UF_NOT_DELEGATED = &H100000

ADS_UF_USE_DES_KEY_ONLY = &H200000

ADS_UF_DONT_REQUIRE_PREAUTH = &H400000

ADS_UF_PASSWORD_EXPIRED = &H800000

ADS_UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION = &H1000000

end enum

end module

Im folgenden Beispiel wird das samAccountName-Attribut festgelegt. Weitere Informationen zu diesem Attribut finden Sie im Thema "samAccountName" in der MSDN Library unter https://go.microsoft.com/fwlink/?LinkID=27252.

Das samAccountName-Attribut erstellt einen eindeutigen SAM-Kontonamen, wie z. B. $CP2000-O16B1V0UKHK7. Dieses Attribut ist im Benutzerkonto erforderlich, wenn der Domänencontroller unter Windows NT Server 4.0 ausgeführt wird. In Windows Server 2003 ist das samAccountName-Attribut optional.

Siehe auch

Referenz

System.DirectoryServices

Konzepte

Benutzerverwaltung

Send comments about this topic to Microsoft.

Copyright © 2007 Microsoft Corporation. Alle Rechte vorbehalten.