Active Directory, enumerating user's groups, COM exception


while enumerating current user's groups through AD .NET API I sometimes get

COMException: Unknown error (0x80005000)

Here's my code :

        var userName = Environment.UserName;

        var context = new PrincipalContext(ContextType.Domain);
        var user = UserPrincipal.FindByIdentity(context, userName);

        foreach (var userGroup in user.GetGroups())

What's the problem? I thought every user can retrieve list of HIS groups?It seems to be strange behavior, sometimes It can be reproduced like this : when running on 'userA' PC, It crashes, but it is enumerating OTHER 'userB' groups successfully (under 'userA')!

asked on Stack Overflow Apr 21, 2011 by illegal-immigrant • edited Mar 1, 2013 by Dan Davies Brackett

3 Answers


Try using

var context = new PrincipalContext(ContextType.Domain, "", "DC=yourcompany,DC=com", ContextOptions.Negotiate);

With the ContextOption set to Negotioate the client is authenticated by using either Kerberos or NTLM so even if the user name and password are not provided the account management API binds to the object by using the security context of the calling thread.

answered on Stack Overflow Apr 27, 2011 by Raymund

0x80005000 = E_ADS_BAD_PATHNAME so you supply an invalid adspath somewhere, maybe you must add LDAP:// prefix or opposit are doing this twice? Set a breakpoint and inspect value...

EDIT: AdsPath should be a value like "LDAP://CN=Administator,CN=Users,DC=contoso,DC=com", you seem to have a misformed path.

answered on Stack Overflow Apr 21, 2011 by Remko • edited Apr 21, 2011 by Remko

I had the same problem, I solved it by supplying the domain name when creating the PrincipalContext:

var domain = new PrincipalContext(ContextType.Domain, Environment.UserDomainName);
var user = UserPrincipal.FindByIdentity(domain, Environment.UserName);
answered on Stack Overflow Sep 24, 2013 by Moinois

User contributions licensed under CC BY-SA 3.0