System.DirectoryServices

N

news.microsoft.com

Hi,
Anyone there who can give me a jump start for retrieving the groups a user
belongs to???

Since now i tried this but keeps on giving an error on line
(listBox1.Items.Add("Groups to which {0} belongs:"+ oUser.Name);
The oUser object is returning an error " AdsObject <error: an exception of
type: {System.Runtime.InteropServices.COMException} occurred>
System.DirectoryServices.Interop.UnsafeNativeMethods.IAds
"

string strUserADsPath = "LDAP://" + System.Environment.UserDomainName +

"/CN=" + textBox1.Text +",CN=users,DC=" + System.Environment.UserDomainName
+

",DC=nl";

DirectoryEntry oUser;

oUser = new DirectoryEntry(strUserADsPath);

listBox1.Items.Add("Groups to which {0} belongs:"+ oUser.Name);

// Invoke IADsUser::Groups method.

object groups = oUser.Invoke("Groups");

foreach ( object group in (IEnumerable)groups)

{

// Get the Directory Entry.

DirectoryEntry groupEntry = new DirectoryEntry(group);

listBox1.Items.Add(groupEntry.Name);



Please help me on thisone.

Regards.

Rene
 
M

Marc Scheuner [MVP ADSI]

Since now i tried this but keeps on giving an error on line
(listBox1.Items.Add("Groups to which {0} belongs:"+ oUser.Name);
The oUser object is returning an error " AdsObject <error: an exception of
type: {System.Runtime.InteropServices.COMException} occurred>
System.DirectoryServices.Interop.UnsafeNativeMethods.IAds
"
string strUserADsPath = "LDAP://" + System.Environment.UserDomainName +
"/CN=" + textBox1.Text +",CN=users,DC=" + System.Environment.UserDomainName
+>",DC=nl";

Check this bind string - I am afraid this might not work as
expected.... the .UserDomainName doesn't always return what you might
think it should....
// Invoke IADsUser::Groups method.
object groups = oUser.Invoke("Groups");
foreach ( object group in (IEnumerable)groups)
{
// Get the Directory Entry.
DirectoryEntry groupEntry = new DirectoryEntry(group);
listBox1.Items.Add(groupEntry.Name);

Why this complicated??? The groups a user belongs to are stored in the
(computed) attribute "memberOf" - just enumerate that!! No need to do
a "Invoke" and other stuff......

foreach(object oGroup in oUser.Properties["memberOf"])
{
listBox1.Items.Add(oGroup.ToString());
}

That should do the trick !

Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top