Export (0) Print
Expand All

DirectoryEntry.Username Property

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

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

Property Value

The user name to use when authenticating the client.

Remarks

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.

Example

[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 = _
      "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 

[C#] 
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);

[C++] 
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);
}

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

Requirements

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

Show:
© 2014 Microsoft