Microsoft KB Archive/327442

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

Article ID: 327442

Article Last Modified on 10/12/2004

-

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 &quot;DirectoryEntry.&quot; 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 &quot;DirectoryEntry,&quot; 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 &quot;searchroot&quot; 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.



Steps to reproduce the problem
 Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. Under Project Types, click Visual C# Projects, and then click Console Application under Templates. Name the project &quot;Q327442.&quot; In Solution Explorer, right-click References, and then click Add Reference. In the Add Reference dialog box, click the .NET tab.</li> Double-click '''System. DirectoryServices.dll under Component Name'''.</li> Make sure that System.DirectoryServices.dll appears under Selected Components, and then click OK.</li>  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(

&quot;LDAP://yourcorp.com/CN=Users,DC=yourcorp,DC=com&quot;,

&quot;USERNAME&quot;,

&quot;PASSWORD&quot;, AuthenticationTypes.Secure );

string[] props = {&quot;cn&quot;};

//TODO: Replace FULL NAME with the name that you want to search for.

DirectorySearcher dirSearch = new DirectorySearcher(

searchRoot,

&quot;sAMAccountName=userID&quot;,

props,

SearchScope.OneLevel );

SearchResult oSearchResult = dirSearch.FindOne;

DirectoryEntry dirEntry = oSearchResult.GetDirectoryEntry;

Console.WriteLine( dirEntry.Name );

Console.Read;

}

}

} </li> Search for the &quot;TODO&quot; text string in the previous code sample. Modify the code sample for your environment as instructed in the &quot;TODO&quot; text string.</li> Press F5 to build and to run the project.

You may receive the error message that is mentioned in the &quot;Symptoms&quot; section.</li></ol>

Additional query words: DirectorySearcher bind

<div class="references_section">