UserPrincipal.FindByIdentity Unable to cast object of type 'GroupPrincipal' to type 'UserPrincipal

960 views Asked by At

Good morning,

i'm trying to find a user in our active directory, but am getting the following exception:

 {System.SystemException} = {"Unable to cast object of type  'System.DirectoryServices.AccountManagement.GroupPrincipal' to type
 'System.DirectoryServices.AccountManagement.UserPrincipal'."}

my code is as follows, and it seems to have worked in the past, not sure what has changed. i've been staring and debuging and can't find the issue, please help.

 using (PrincipalContext ctx = new PrincipalContext(
                ContextType.Domain, host, uContext))
            {

                if (ctx != null)
                {

                    using(UserPrincipal p = UserPrincipal.FindByIdentity(ctx,IdentityType.SamAccountName, username))
                    {

where uContext is: "CN=Users,DC=my,DC=eh,DC=tech,DC=com" and host is: my.eh.tech.com and the username is:madamzuzu

when i look for the user in the AD, they are shown correctly with the following DN:CN=madamzuzu;CN=Users,DC=my,DC=eh,DC=tech,DC=com

i can't understand where the "groupprincipal" is coming from?

0

There are 0 answers