UserPrincipal.GetGroups throws System.DirectoryServices.ActiveDirectory.ActiveDirectoryObjectNotFoundException RRS feed

  • Question

  • I wrote a web application which uses Active Directory user info for identiry authentication and Groups info for role access control. but I found it is instable. 

    I used the code as follow.

       UserPrincipal usr = GetUserPrincipal(samAccountName); // a function get a UserPrincepal instance from Active Directory
       if(usr != null)
          PrincipalSearchResult<Principal> oPrincipalSearchResult = usr.GetGroups();
          foreach (Principal oResult in oPrincipalSearchResult)
    catch (Exception e)

    When user logs in , it shows the appropriate menu, but some times it shows a empty menu, and it is fine in a little time.

    I debbug it and findd when it step into 

    PrincipalSearchResult<Principal> oPrincipalSearchResult = usr.GetGroups();

    it throws a System.DirectoryServices.ActiveDirectory.ActiveDirectoryObjectNotFoundException.

    The Active Directory Server runs  Microsoft Windows Server 2008 R2 (64bit)

    Friday, January 3, 2014 3:31 AM


  • Hi Seamas,

    Try these code below first, it works on my side.

    using (var ctx = new PrincipalContext(ContextType.Domain))

                using (var userPrincipal = UserPrincipal.FindByIdentity(ctx, user))


                    PrincipalSearchResult<Principal> result = userPrincipal.GetGroups();

                    GroupPrincipal groupPrincipal =

                         result.Where(g => g.SamAccountName == groupName).FirstOrDefault();

                    return (groupPrincipal != null);


    If it doesn’t work, please refer to the following link to see a hotfix about it, follow the steps and let me know the result.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, January 6, 2014 1:35 AM