Share via


DirectoryEntry.Username

The Username property gets or sets the user name to use when authenticating the client.

public String Username {get; set;}
Public Property Username As String

Property Value

The user name to use when authenticating the client.

Remarks

You can set the Username 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.

Example Code [Visual Basic .NET]

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.

Dim myADSPath As String = _
      "LDAP://onecity/CN=Users,DC=onecity,DC=corp,DC=fabrikam,DC=com"

' 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
   Console.WriteLine(myChildDirectoryEntry.Path)
Next myChildDirectoryEntry

Example Code [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.

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)
   Console.WriteLine(myChildDirectoryEntry.Path);

Example Code [Managed 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.

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);
    Console::WriteLine(myChildDirectoryEntry->Path);
}

Requirements

Client Requires Windows XP Home Edition, Windows XP Professional, Windows Me, or Windows 98.
Server Requires Windows Server 2003, Windows 2000, or Windows NT 4.0.
Namespace

Defined in System.DirectoryServices.

Assembly

Requires System.DirectoryServices (in System.DirectoryServices.dll).

.NET Framework

Requires .NET Framework 1.0.