属于域的用户显示组用户

2023-09-08 13:03:49 作者:退骚药

我有2用户一个在本地计算机和一个域:用户1和放大器; TESTDOMAIN \ USER1

I have 2 users one on local machine and one on domain.: user1 & testdomain\user1

现在这两个用户有不同的组

Now both these users have different groups

用户1 = 1组,第3组

user1 = group1, group3

TESTDOMAIN \ USER1 =第2组,组4

testdomain\user1 = group2, group4

现在我想显示这些群体,我没有烦恼显示的用户1组,但我不能显示TESTDOMAIN \ USER1组。

now i wish to display these groups, i have no trouble displaying the groups of user1 but i cannot display the groups of testdomain\user1.

我的code是如下。

DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
DirectoryEntry user = AD.Children.Find(completeUserName, "user");
object obGroups = user.Invoke("Groups");

foreach (object ob in (IEnumerable)obGroups)
{
    // Create object for each group.
    DirectoryEntry obGpEntry = new DirectoryEntry(ob);
    listOfMyWindowsGroups.Add(obGpEntry.Name);
}

在这里completeusername = user1和TESTDOMAIN \ USER1

where completeusername = user1 and testdomain\user1

有什么建议?谢谢

推荐答案

如果您使用的是.NET 3.5或更高版本,然后看看的 System.DirectoryServices.AccountManagement 。这些类是远远易于使用。例如,

If you are using .NET 3.5 or later then have a look at System.DirectoryServices.AccountManagement. These classes are far easy to work with. For example,

PrincipalContext pc = new PrincipalContext(ContextType.Domain);
UserPrincipal user = UserPrincipal.FindByIdentity(pc, "johndoe");
var groups = user.GetAuthorizationGroups()  // or user.GetUserGroups() 

如果它的机器的用户,那么你必须使用 ContextType.Machine

If its machine user then you have to use ContextType.Machine

也看看这些文章,给出了相同的一点概述:

Also have a look at these article that gives a bit of overview for the same:

HTTP://anyrest.word$p $ pss.com / 2010/06/28 /主动目录-C /

http://msdn.microsoft.com/en-us/杂志/ cc135979.aspx#S5