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
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
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.
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 ) ;
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 "Q327442."
- In Solution Explorer, right-click References, and then click Add Reference.
- In the Add Reference dialog box, click the .NET tab.
- Double-click System. DirectoryServices.dll under Component Name.
- Make sure that System.DirectoryServices.dll appears under Selected Components, and then click OK.
- 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();
}
}
}
- 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.
- Press F5 to build and to run the project.
You may receive the error message that is mentioned in the "Symptoms" section.
For additional information about the
DirectorySearcher class, visit the following Microsoft Developer Network (MSDN) Web site:
APPLIES TO
- Microsoft .NET Framework 1.0
- Microsoft Visual Studio .NET 2002 Professional Edition
| 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