Psst! Did you know DevelopmentNow is a mobile web site design agency?

Contact us for help mobilizing your site, or to sign up for our beta Mobile Web SDK!
all groups > dotnet security > may 2005 >

dotnet security : User Groups


Nathan
5/31/2005 12:55:13 AM
I am attempting for find the groups that a user is associated with but all
the examples I come across don't work. I'm unsure as to whether it is due to
permissions on our network or if I'm constructing the path to use incorrectly.

Below is C# code I found that should list the groups for a given user, I've
ommitted the actual values for my network for obvious reasons, instead I've
used a domain which would be abc.def.net as the domain I am accessing.

Any help on either why this is not working or examples that I can use would
be great.

Nathan

string strUserADsPath = "LDAP://abc.def.net/cn=" +textBox1.Text
+",cn=users,dc=abc,dc=def,dc=net";
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);
Joe Kaplan (MVP - ADSI)
5/31/2005 9:15:27 AM
That approach won't get you all the groups for the user (nested membership
and primary group will be excluded), but it should work. What error are you
getting (full stace trace)?

Joe K.

[quoted text, click to view]

Nathan
5/31/2005 3:49:39 PM
The error occurs on the line listBox1.Items.Add("Groups to which {0}
belongs:"+ oUser.Name);

Stack trace is as follows

"System.Runtime.InteropServices.COMException (0x80072030): There is no such
object on the server
at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
at System.DirectoryServices.DirectoryEntry.Bind()
at System.DirectoryServices.DirectoryEntry.get_Name()
at Groups.Form1.button1_Click(Object sender, EventArgs e) in
c:\\dev\\visual studio projects\\groups\\form1.cs:line 135"

I know our domain is ActiveDirectory as our existing VB6 app uses similar
code to retrieve users groups, what COM object is it trying to get?


[quoted text, click to view]
Joe Kaplan (MVP - ADSI)
5/31/2005 7:39:53 PM
You usually get that error when the DN (the part after the DNS name) you
specified doesn't point to a real object. Are you sure the DN in your path
is correct?

Joe K.

[quoted text, click to view]

Nathan
6/2/2005 5:07:28 AM
Joe, sorry to sound thick but which part of the string is the DNS name?

[quoted text, click to view]
Joe Kaplan (MVP - ADSI)
6/2/2005 5:31:07 PM
The ADS Path is basically:
<scheme>://<server>/<object>

scheme is LDAP or GC for LDAP databases. Server is optional for AD and can
be lots of things like the DNS name of a DC, the DNS name of a domain, an IP
address or a netbios name (or blank if doing serverless binding, which finds
a DC based on the current thread's security context's domain membership).
It can also contain a port if the default port is not right. The object
part is an LDAP distinguished name or other special identifier known by the
directory. If it is null, it tries to find the defaultNamingContext.

HTH,

Joe K.

[quoted text, click to view]

AddThis Social Bookmark Button