HighTechTalks DotNet Forums  

DirectoryServices bug?

Dotnet General Discussions microsoft.public.dotnet.general


Discuss DirectoryServices bug? in the Dotnet General Discussions forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
bugnthecode
 
Posts: n/a

Default DirectoryServices bug? - 11-15-2006 , 07:59 PM






Hi, I'm building a small desktop app in VS Std 2005 with C# and .net
2.0. I've managed to get the code together to query the ldap my company
has, but every time I attempt to access a specific property a COM
Exception gets thrown, and I can't figure out why. This is a desktop
app.

example("ldap.example.com", "ou=People,dc=example,dc=com");

public void example(string server, string ou) {
searcher = new DirectorySearcher(new DirectoryEntry("LDAP://" + server
+ "/" + ou, "", "", AuthenticationTypes.ReadonlyServer));

searcher.Filter = "(uid=wsmith)";
SearchResultCollection results = searcher.FindAll();
Console.WriteLine((string)results[0].GetDirectoryEntry().Properties["gPhotoID"]);
// OFFENDING LINE!
}

So that's pretty much the smallest bit of code I can use and get the
error. All the other properties I've tried to access so far have worked
just fine, I've even found some properties to be a collection of values
which works just fine. The gPhotoID should contain a url to a picture
which would look like
"http://intranet.example.com/employees/pictures/18273010.JPG".

The error that I'm getting is:
System.Runtime.InteropServices.COMException was unhandled
Message="Unknown error (0x8000500c)"
Source="System.DirectoryServices"
ErrorCode=-2147463156
StackTrace:
at
System.DirectoryServices.PropertyValueCollection.P opulateList()
at
System.DirectoryServices.PropertyValueCollection.. ctor(DirectoryEntry
entry, String propertyName)
at
System.DirectoryServices.PropertyCollection.Proper tyEnumerator.get_Entry()
at
System.DirectoryServices.PropertyCollection.Proper tyEnumerator.get_Current()
at UserManager.UserFactory.Find(String criteria) in D:\My
Documents\Visual Studio
2005\Projects\UserManager\UserManager\UserFactory. cs:line 58
at UserManager.UserFactory.FindByUnix(String unix) in D:\My
Documents\Visual Studio
2005\Projects\UserManager\UserManager\UserFactory. cs:line 26
at UserManagerTest.Program.Main(String[] args) in D:\My
Documents\Visual Studio
2005\Projects\UserManagerTest\UserManagerTest\Prog ram.cs:line 10
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at System.AppDomain.ExecuteAssembly(String assemblyFile,
Evidence assemblySecurity, String[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.Run UsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context( Object
state)
at System.Threading.ExecutionContext.Run(ExecutionCon text
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

I can't seem to find a solution. I've been googling for over an hour,
and most of the answers I find pertain to credential issues, and
asp.net issues. Can someone please offer assistance?

Thanks,
Will


Reply With Quote
  #2  
Old   
Marc Scheuner
 
Posts: n/a

Default Re: DirectoryServices bug? - 11-16-2006 , 12:48 AM






Quote:
example("ldap.example.com", "ou=People,dc=example,dc=com");

public void example(string server, string ou) {
searcher = new DirectorySearcher(new DirectoryEntry("LDAP://" + server
+ "/" + ou, "", "", AuthenticationTypes.ReadonlyServer));

searcher.Filter = "(uid=wsmith)";
SearchResultCollection results = searcher.FindAll();
Console.WriteLine((string)results[0].GetDirectoryEntry().Properties["gPhotoID"]);
// OFFENDING LINE!
Well, there are two problems here:

1) The "searcher.FindAll()" call could possibly return no results, if
your filter doesn't match any records - so blindly using "results[0]"
will cause an exception when no result has been returned

2) Along similar lines - even if you have one or multiple results
returned, those possibly will not contain any value in "gPhotoID"
(because the path has not been set), and then using that (by casting
it to a string) will also cause an exception.

Furthermore - if you want to access just a single property (or a few)
on the search result, your best bet is to specify those properties on
the searcher object itself and query them from the result directly
(without having to retrieve the full "DirectoryEntry" object and
access them there).

So your search should look something like this:

searcher.Filter = "(uid=wsmith)";
// specify which properties to load directly into the result
searcher.PropertiesToLoad.Add("gPhotoID");

SearchResultCollection results = searcher.FindAll();

// check to see if we have any results !
if(results.Count > 0)
{
SearchResult firstResult = results[0];

// check to see if result contains a value for the property
if(firstResult.Properties.Contains["gPhotoID"])
{
// the result's property might be a multi-value
// string property, so pick string [0] from the
// collection
string photoID =
firstResult.Properties["gPhotoID"][0].ToString();
}
}

HTH
Marc


Reply With Quote
  #3  
Old   
bugnthecode
 
Posts: n/a

Default Re: DirectoryServices bug? - 11-16-2006 , 10:49 AM



Thanks for your reply. I know that it's possible to not get any results
back from searcher.FindAll(), but I was trying to show the smallest
possible example. I have tried using the ProperitesToLoad.Add() and
when I do even the properties I don't specify are actually accessible.
Do you know why this would happen? I'll try it again specifying every
property I would like to load, and checking for multiple values for
each property. I just don't understand why that specific property
throws an exception when none of the others do. Could it be the format
that the string is in (in the format for a url?)? Would the length have
to do with it (the length of that property for myself in the directory
is 58 characters)? And what exactly does that exception mean?

Thanks for your help Marc,
Will

Marc Scheuner wrote:
Quote:
example("ldap.example.com", "ou=People,dc=example,dc=com");

public void example(string server, string ou) {
searcher = new DirectorySearcher(new DirectoryEntry("LDAP://" + server
+ "/" + ou, "", "", AuthenticationTypes.ReadonlyServer));

searcher.Filter = "(uid=wsmith)";
SearchResultCollection results = searcher.FindAll();
Console.WriteLine((string)results[0].GetDirectoryEntry().Properties["gPhotoID"]);
// OFFENDING LINE!

Well, there are two problems here:

1) The "searcher.FindAll()" call could possibly return no results, if
your filter doesn't match any records - so blindly using "results[0]"
will cause an exception when no result has been returned

2) Along similar lines - even if you have one or multiple results
returned, those possibly will not contain any value in "gPhotoID"
(because the path has not been set), and then using that (by casting
it to a string) will also cause an exception.

Furthermore - if you want to access just a single property (or a few)
on the search result, your best bet is to specify those properties on
the searcher object itself and query them from the result directly
(without having to retrieve the full "DirectoryEntry" object and
access them there).

So your search should look something like this:

searcher.Filter = "(uid=wsmith)";
// specify which properties to load directly into the result
searcher.PropertiesToLoad.Add("gPhotoID");

SearchResultCollection results = searcher.FindAll();

// check to see if we have any results !
if(results.Count > 0)
{
SearchResult firstResult = results[0];

// check to see if result contains a value for the property
if(firstResult.Properties.Contains["gPhotoID"])
{
// the result's property might be a multi-value
// string property, so pick string [0] from the
// collection
string photoID =
firstResult.Properties["gPhotoID"][0].ToString();
}
}

HTH
Marc


Reply With Quote
  #4  
Old   
bugnthecode
 
Posts: n/a

Default Re: DirectoryServices bug? - 11-16-2006 , 02:02 PM



Ok so I tried it again with specifying each property before performing
the search, and checking for multiple results as specified before. I
still get the same exception. I've tried accessing other properties
that are available, but I don't need and I can seem to access those
just fine, but every time I get to this one it throws an exception. Any
Ideas??

Thanks,
Will

bugnthecode wrote:
Quote:
Thanks for your reply. I know that it's possible to not get any results
back from searcher.FindAll(), but I was trying to show the smallest
possible example. I have tried using the ProperitesToLoad.Add() and
when I do even the properties I don't specify are actually accessible.
Do you know why this would happen? I'll try it again specifying every
property I would like to load, and checking for multiple values for
each property. I just don't understand why that specific property
throws an exception when none of the others do. Could it be the format
that the string is in (in the format for a url?)? Would the length have
to do with it (the length of that property for myself in the directory
is 58 characters)? And what exactly does that exception mean?

Thanks for your help Marc,
Will



Reply With Quote
  #5  
Old   
Marc Scheuner
 
Posts: n/a

Default Re: DirectoryServices bug? - 11-17-2006 , 01:05 AM



Quote:
Ok so I tried it again with specifying each property before performing
the search, and checking for multiple results as specified before. I
still get the same exception. I've tried accessing other properties
that are available, but I don't need and I can seem to access those
just fine, but every time I get to this one it throws an exception. Any
Ideas??
Are you still using the ".GetDirectoryEntry()" call?? This will return
the complete DirectoryEntry for the search result - that will
obviuosly have all the properties available !

Marc


Reply With Quote
  #6  
Old   
ssg31415926
 
Posts: n/a

Default Re: DirectoryServices bug? - 11-22-2006 , 09:21 AM



Have you seen this posting: "Errors Reading IBM ITIM LDAP properties"
in microsoft.public.adsi.general ? It doesn't exactly apply to your
situation as it's about non-AD LDAP directories but maybe gPhoto*ID is
as poorly handled by the ADSi-.NET conversion as non-AD attributes?

SSG


Reply With Quote
  #7  
Old   
wayne_bradney@yahoo.com
 
Posts: n/a

Default Re: DirectoryServices bug? - 12-01-2006 , 09:50 AM




bugnthecode wrote:
Quote:
Hi, I'm building a small desktop app in VS Std 2005 with C# and .net
2.0. I've managed to get the code together to query the ldap my company
has, but every time I attempt to access a specific property a COM
Exception gets thrown, and I can't figure out why. This is a desktop
app.

example("ldap.example.com", "ou=People,dc=example,dc=com");

public void example(string server, string ou) {
searcher = new DirectorySearcher(new DirectoryEntry("LDAP://" + server
+ "/" + ou, "", "", AuthenticationTypes.ReadonlyServer));

searcher.Filter = "(uid=wsmith)";
SearchResultCollection results = searcher.FindAll();
Console.WriteLine((string)results[0].GetDirectoryEntry().Properties["gPhotoID"]);
// OFFENDING LINE!
}

So that's pretty much the smallest bit of code I can use and get the
error. All the other properties I've tried to access so far have worked
just fine, I've even found some properties to be a collection of values
which works just fine. The gPhotoID should contain a url to a picture
which would look like
"http://intranet.example.com/employees/pictures/18273010.JPG".

The error that I'm getting is:
System.Runtime.InteropServices.COMException was unhandled
Message="Unknown error (0x8000500c)"
Source="System.DirectoryServices"
ErrorCode=-2147463156
StackTrace:
at
System.DirectoryServices.PropertyValueCollection.P opulateList()
at
System.DirectoryServices.PropertyValueCollection.. ctor(DirectoryEntry
entry, String propertyName)
at
System.DirectoryServices.PropertyCollection.Proper tyEnumerator.get_Entry()
at
System.DirectoryServices.PropertyCollection.Proper tyEnumerator.get_Current()
at UserManager.UserFactory.Find(String criteria) in D:\My
Documents\Visual Studio
2005\Projects\UserManager\UserManager\UserFactory. cs:line 58
at UserManager.UserFactory.FindByUnix(String unix) in D:\My
Documents\Visual Studio
2005\Projects\UserManager\UserManager\UserFactory. cs:line 26
at UserManagerTest.Program.Main(String[] args) in D:\My
Documents\Visual Studio
2005\Projects\UserManagerTest\UserManagerTest\Prog ram.cs:line 10
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at System.AppDomain.ExecuteAssembly(String assemblyFile,
Evidence assemblySecurity, String[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.Run UsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context( Object
state)
at System.Threading.ExecutionContext.Run(ExecutionCon text
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

I can't seem to find a solution. I've been googling for over an hour,
and most of the answers I find pertain to credential issues, and
asp.net issues. Can someone please offer assistance?

Thanks,
Will
I'm getting the exact same problem. I can read all properties in the
entire directory except for two specific ones which coincidentally were
only recently added to the schema. For those two properties I can't
even enumerate them my loop. As soon as the loop steps to those
properties, boom. Still investigating.



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.