Microsoft Knowledge Base Email Alertz

(327442) - Describes you receive a 'System.Runtime.InteropServices.COMException' occurred in system.directoryservices.dll error message when you use DirectoryEntry.Name.after you bind the entry by using DirectorySearcher.FindOne ().GetDirectoryEntry() method.

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 327442 - Last Review: October 12, 2004 - Revision: 1.1

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

This article was previously published under Q327442

On This Page

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:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDirectoryServicesDirectorySearcherClassTopic.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDirectoryServicesDirectorySearcherClassTopic.asp)

APPLIES TO
  • Microsoft .NET Framework 1.0
  • Microsoft Visual Studio .NET 2002 Professional Edition
Keywords: 
kberrmsg kbtshoot kbprogramming kbprb KB327442
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate