The specified forest does not exist or cannot be contacted

I was trying to get the active directory group details of a user for me to authenticate an Application. The scenario is that when the server machine lives in another domain than the domain your machine is connected to.

The following code was works fine when i am in the same domain as the concerned domain which i am testing the application:

/// <summary>
        /// Gets a list of the users group memberships
        /// </summary>
        /// <param name="sUserName">The user you want to get the group memberships</param>
        /// <returns>Returns an arraylist of group memberships</returns>
        public static ArrayList GetUserGroups(string sUserName)
        {
            ArrayList myItems = new ArrayList();
            UserPrincipal oUserPrincipal = GetUser(sUserName);

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

            foreach (Principal oResult in oPrincipalSearchResult)
            {
                myItems.Add(oResult.Name);
            }
            return myItems;
        }

if not it means when the domain is different, you will get the error “The specified forest does not exist or cannot be contacted.”.

for you to sort out the above error assuming I am in a different domain at the same time the testing domain is different:

/// <summary>
        /// Gets a list of the users group memberships
        /// </summary>
        /// <param name="sUserName">The user you want to get the group memberships</param>
        /// <returns>Returns an arraylist of group memberships</returns>
        public static ArrayList GetUserGroups(string sUserName)
        {

            ArrayList myItems = new ArrayList();
            UserPrincipal oUserPrincipal = GetUser(sUserName);

            PrincipalSearchResult<Principal> groups = oUserPrincipal.GetAuthorizationGroups();
            var iterGroup = groups.GetEnumerator();
            using (iterGroup)
            {
                while (iterGroup.MoveNext())
                {
                    try
                    {
                        Principal p = iterGroup.Current;
                        myItems.Add(p.Name);
                    }
                    catch (NoMatchingPrincipalException pex)
                    {
                        continue;
                    }
                }
            }
            return myItems;
        }

Leave a comment