A
Andy
Hi,
I'm having a problem querying active directory. It happens so far
only on one computer under a particular user account. Things seem to
work fine for that user on another computer, and if I logon to her
computer the query works fine as well. here is the C# code I'm using
to do the query:
private static DirectoryEntry GetUserEntry( WindowsIdentity
ident ) {
string directoryServer;
DirectorySearcher searcher;
DirectoryEntry domainEntry, result;
SearchResult results;
string userName;
result = null;
using ( domainEntry = new DirectoryEntry( "LDAP://rootdse" ) ) {
directoryServer = domainEntry.Invoke(
"GET",
"defaultNamingContext"
).ToString();
}
if ( string.IsNullOrEmpty( directoryServer ) ) {
throw new InvalidOperationException(
Properties.Resources.CannotDetermineDirectoryServer
);
}
if ( directoryServer.StartsWith( @"\\" ) ) {
directoryServer = directoryServer.Substring( 2 );
}
using ( domainEntry = new DirectoryEntry(
string.Format( "LDAP://{0}", directoryServer ) ) ) {
userName = ident.Name;
if ( userName.IndexOf( @"\" ) > -1 ) {
userName =
userName.Substring( userName.IndexOf( @"\" ) + 1 );
}
using (
searcher = new DirectorySearcher(
domainEntry,
string.Format( "SAMAccountName={0}", userName )
)
) {
results = searcher.FindOne();
result = results.GetDirectoryEntry();
}
}
return result;
}
The initial part seems to succeed (querying for the rootdse), but
finding the SAMAccount is failing for some reason. Any ideas? Here's
the exception:
System.Runtime.InteropServices.COMException (0x80072020): An
operations error occurred.
at System.DirectoryServices.DirectoryEntry.Bind(Boolean
throwIfFail)
at System.DirectoryServices.DirectoryEntry.Bind()
at System.DirectoryServices.DirectoryEntry.get_AdsObject()
at System.DirectoryServices.DirectorySearcher.FindAll(Boolean
findMoreThanOne)
at System.DirectoryServices.DirectorySearcher.FindOne()
at MyCompan.Application.UserInfo.GetUserEntry(WindowsIdentity
ident)
I'm having a problem querying active directory. It happens so far
only on one computer under a particular user account. Things seem to
work fine for that user on another computer, and if I logon to her
computer the query works fine as well. here is the C# code I'm using
to do the query:
private static DirectoryEntry GetUserEntry( WindowsIdentity
ident ) {
string directoryServer;
DirectorySearcher searcher;
DirectoryEntry domainEntry, result;
SearchResult results;
string userName;
result = null;
using ( domainEntry = new DirectoryEntry( "LDAP://rootdse" ) ) {
directoryServer = domainEntry.Invoke(
"GET",
"defaultNamingContext"
).ToString();
}
if ( string.IsNullOrEmpty( directoryServer ) ) {
throw new InvalidOperationException(
Properties.Resources.CannotDetermineDirectoryServer
);
}
if ( directoryServer.StartsWith( @"\\" ) ) {
directoryServer = directoryServer.Substring( 2 );
}
using ( domainEntry = new DirectoryEntry(
string.Format( "LDAP://{0}", directoryServer ) ) ) {
userName = ident.Name;
if ( userName.IndexOf( @"\" ) > -1 ) {
userName =
userName.Substring( userName.IndexOf( @"\" ) + 1 );
}
using (
searcher = new DirectorySearcher(
domainEntry,
string.Format( "SAMAccountName={0}", userName )
)
) {
results = searcher.FindOne();
result = results.GetDirectoryEntry();
}
}
return result;
}
The initial part seems to succeed (querying for the rootdse), but
finding the SAMAccount is failing for some reason. Any ideas? Here's
the exception:
System.Runtime.InteropServices.COMException (0x80072020): An
operations error occurred.
at System.DirectoryServices.DirectoryEntry.Bind(Boolean
throwIfFail)
at System.DirectoryServices.DirectoryEntry.Bind()
at System.DirectoryServices.DirectoryEntry.get_AdsObject()
at System.DirectoryServices.DirectorySearcher.FindAll(Boolean
findMoreThanOne)
at System.DirectoryServices.DirectorySearcher.FindOne()
at MyCompan.Application.UserInfo.GetUserEntry(WindowsIdentity
ident)