This documentation is archived and is not being maintained.

DirectoryEntry.Username Property

Gets or sets the user name to use when authenticating the client.

[Visual Basic]
Public Property Username As String
public string Username {get; set;}
public: __property String* get_Username();
public: __property void set_Username(String*);
public function get Username() : String;
public function set Username(String);

Property Value

The user name to use when authenticating the client.


You can set the user name and Password in order to specify alternate credentials with which to access the information in Active Directory. Any other DirectoryEntry objects retrieved from this instance (for example, through Children) are automatically created with the same alternate credentials.


[Visual Basic, C#, C++] The following example demonstrates the Username and Password properties of the DirectoryEntry class. This example gets the user-specified DirectoryEntry and the Path of all its Children.

[Visual Basic] 
Dim myADSPath As String = _

' Create an Instance of DirectoryEntry.
Dim myDirectoryEntry As New DirectoryEntry(myADSPath)
myDirectoryEntry.Username = UserName
myDirectoryEntry.Password = SecurelyStoredPassword

' Get the Child ADS objects.
Console.WriteLine("The Child ADS objects are:")
Dim myChildDirectoryEntry As DirectoryEntry
For Each myChildDirectoryEntry In  myDirectoryEntry.Children
Next myChildDirectoryEntry 

String myADSPath = "LDAP://onecity/CN=Users,DC=onecity,DC=corp,DC=fabrikam,DC=com";
// Create an Instance of DirectoryEntry.
DirectoryEntry myDirectoryEntry = new DirectoryEntry(myADSPath);
myDirectoryEntry.Username = UserName;
myDirectoryEntry.Password = SecurelyStoredPassword;

// Get the Child ADS objects.
Console.WriteLine("The Child ADS objects are:");
foreach(DirectoryEntry myChildDirectoryEntry in myDirectoryEntry.Children)

String* myADSPath = S"LDAP://onecity/CN=Users,DC=onecity,DC=corp,DC=fabrikam,DC=com";

// Create an Instance of DirectoryEntry.
DirectoryEntry* myDirectoryEntry = new DirectoryEntry(myADSPath);
myDirectoryEntry->Username = UserName;
myDirectoryEntry->Password = SecurelyStoredPassword;

// Get the Child ADS objects.
Console::WriteLine(S"The Child ADS objects are:");
Collections::IEnumerator* myEnum = myDirectoryEntry->Children->GetEnumerator();
while (myEnum->MoveNext()) {
    DirectoryEntry* myChildDirectoryEntry = __try_cast<DirectoryEntry*>(myEnum->Current);

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

DirectoryEntry Class | DirectoryEntry Members | System.DirectoryServices Namespace | Children | Password | AuthenticationType