You cannot use the DirectorySearcher.FindOne().GetDirectoryEntry() method to bind a new DirectoryEntry object in the .NET Framework 1.0 (327442)



The information in this article applies to:

  • Microsoft .NET Framework 1.0
  • Microsoft Visual Studio .NET (2002), Professional Edition

This article was previously published under Q327442

SYMPTOMS

A problem may occur when you try to use the DirectoryEntry.Name property after you define a "DirectoryEntry." The following code example shows the code that makes the problem occur.
DirectoryEntry dirEntry = dirSearch.FindOne().GetDirectoryEntry();
When you try to use the DirectoryEntry.Name property after you define a "DirectoryEntry," you may receive the following error message:
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in system.directoryservices.dll

Additional information: The server is not operational

CAUSE

This problem may occur if the System.DirectoryServices call is run with insufficient permissions.

The DirectorySearcher class runs under the credentials that are specified in the DirectoryEntry object that is used as the "searchroot" of the DirectorySearcher class. However, these credentials are not used when the SearchResult.GetDirectoryEntry call is made. The SearchResult.GetDirectoryEntry call is made by using the security context of the calling process.

WORKAROUND

To work around this problem, locate the following code example.
DirectoryEntry dirEntry = dirSearch.FindOne().GetDirectoryEntry();
Replace the previous code example with the following code example.
DirectoryEntry dirEntry = new DirectoryEntry( dirSearch.FindOne().Path, searchRoot.Username, searchRoot.Password, searchRoot.AuthenticationType ) ;

STATUS

This behavior is by design.

This problem is resolved in the Microsoft .NET Framework 1.1.

MORE INFORMATION

Steps to reproduce the problem

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. Under Project Types, click Visual C# Projects, and then click Console Application under Templates. Name the project "Q327442."
  4. In Solution Explorer, right-click References, and then click Add Reference.
  5. In the Add Reference dialog box, click the .NET tab.
  6. Double-click System. DirectoryServices.dll under Component Name.
  7. Make sure that System.DirectoryServices.dll appears under Selected Components, and then click OK.
  8. Put the following code sample in Class1.cs instead of the existing code.
    using System;
    
    using System.DirectoryServices;
    
    namespace Q327442
    
    {
    
    class Class1
    
    {
    
    [STAThread]
    
    static void Main(string[] args)
    
    {
    
    //TODO: Use your own Domain name, your own USERNAME, and your own PASSWORD 
    
    DirectoryEntry searchRoot = new DirectoryEntry(
    
    "LDAP://yourcorp.com/CN=Users,DC=yourcorp,DC=com",
    
    "USERNAME",
    
    "PASSWORD", AuthenticationTypes.Secure );
    
    string[] props = {"cn"};
    
    //TODO: Replace FULL NAME with the name that you want to search for.
    
    DirectorySearcher dirSearch = new DirectorySearcher(
    
    searchRoot,
    
    "sAMAccountName=userID",
    
    props,
    
    SearchScope.OneLevel );
    
    SearchResult oSearchResult = dirSearch.FindOne();
    
    DirectoryEntry dirEntry = oSearchResult.GetDirectoryEntry();
    
    Console.WriteLine( dirEntry.Name );
    
    Console.Read();
    
    }
    
    }
    
    }
    
    
  9. Search for the "TODO" text string in the previous code sample. Modify the code sample for your environment as instructed in the "TODO" text string.
  10. Press F5 to build and to run the project.

    You may receive the error message that is mentioned in the "Symptoms" section.

REFERENCES

For additional information about the DirectorySearcher class, visit the following Microsoft Developer Network (MSDN) Web site:

Modification Type:MajorLast Reviewed:10/12/2004
Keywords:kbtshoot kberrmsg kbProgramming kbprb KB327442 kbAudDeveloper