开发者

display groups belonging to domain users

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

Now both these users have different groups

user1 = group1, group3

testdomain\user1 = group2, group4

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.

My code is as below.

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 ob开发者_运维百科GpEntry = new DirectoryEntry(ob);
    listOfMyWindowsGroups.Add(obGpEntry.Name);
}

where completeusername = user1 and testdomain\user1

Any suggestions?


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() 

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.wordpress.com/2010/06/28/active-directory-c/

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜