C# 如何访问父域中用户的组?

C# 如何访问父域中用户的组?,c#,active-directory,active-directory-group,C#,Active Directory,Active Directory Group,我在一个实例中工作,其中有两个广告域Dom1和Dom2。Dom1与Dom2之间存在单向信任,因此Dom1中的用户可以在Dom2中获得授权 我的c#代码做得很好 然而,在Dom2中,当我从Dom1中的用户中提取用户的组时,我什么也得不到。我确实从Dom2中存在的用户那里获得了一个组列表 _DE.Path = "LDAP://RootDSE"; string szDomain = (string)_DE.Properties["defaultNamingCon

我在一个实例中工作,其中有两个广告域Dom1和Dom2。Dom1与Dom2之间存在单向信任,因此Dom1中的用户可以在Dom2中获得授权

我的c#代码做得很好

然而,在Dom2中,当我从Dom1中的用户中提取用户的组时,我什么也得不到。我确实从Dom2中存在的用户那里获得了一个组列表

            _DE.Path = "LDAP://RootDSE";
        string szDomain = (string)_DE.Properties["defaultNamingContext"][0];
        string obEntry = "LDAP://" + szDomain;
        SearchResult res = ADExists("UserPrincipalName=" + szUPN, "User");
        try
        {
            if (res != null)
            {
                _DE.Path = res.Path;
                //szUserDN = res.Path;
                if (_DE.Properties["memberOf"].Count > 1)
                {
                    object[] groups = (object[])_DE.Properties["memberOf"].Value;
                    if (groups != null)
                    {
                        foreach (object group in groups)
                        {
                            string szGroup = group.ToString();
                            DataRow drAdd = dtGroups.NewRow();
                            drAdd["GroupName"] = group;
                            dtGroups.Rows.Add(drAdd);
                        }
                    }
                }

尝试改用
System.DirectoryServices.AccountManagement
命名空间:

static GroupPrincipal[] GetUserAuthorisationGroups(string userPrincipalName)
{
    using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
    using (UserPrincipal user = UserPrincipal.FindByIdentity(context, IdentityType.UserPrincipalName, userPrincipalName))
    {
        return user.GetAuthorizationGroups().OfType<GroupPrincipal>().ToArray();
    }
}

仍然存在查找用户的问题。似乎有问题的一行是使用(UserPrincipal user=UserPrincipal.findbyitenty……)@ScottJohnson是
szUPN
一个实际的完全合格的UPN,所以
username@domain.local
或者不管dns域名是什么?是的,它看起来像username@domain.localI可以从单向受信任域对上的用户进行授权,但即使指定了用户的位置,我也无法找到/访问该用户。。。
GroupPrincipal[] groups = GetUserAuthorisationGroups(szUPN);

bool searchBySid = groups.Any(g => g.Sid == groupSid);
bool searchByDN = groups.Any(g => g.DistinguishedName == groupDN);
bool searchByName = groups.Any(g => g.Name == groupName);