1

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?

Madam Zu Zu
  • 6,437
  • 19
  • 83
  • 129
  • Just a stupid question - are you sure that `CN=madamzuzu;CN=Users,DC=my,DC=eh,DC=tech,DC=com` points to a user and not just a container? What happens if you use `GroupPrincipal.FindByIdentity` instead? – smoksnes Aug 24 '16 at 14:01
  • Check your input parameters ? – Versatile Aug 24 '16 at 14:13
  • which input parameters? i've debugged it and went through every single variable. all looks ok :( – Madam Zu Zu Aug 24 '16 at 15:35

0 Answers0