Determining members of local groups via C#

后端 未结 6 1625
执念已碎
执念已碎 2020-12-15 15:13

I wondered whether anybody knows how to obtain membership of local groups on a remote server programmatically via C#. Would this require administrator permissions? And if so

6条回答
  •  无人及你
    2020-12-15 15:47

    It appears there is a new Assembly in .net 3.5 called System.DirectoryServices.AccountManagement which gives a cleaner implementation than System.DirectoryServices. Dominick Baier blogs about a couple of simple operations including checking membership of a group:-

    public static bool IsUserInGroup(string username, string groupname, ContextType type)
    {
        PrincipalContext context = new PrincipalContext(type);
    
        UserPrincipal user = UserPrincipal.FindByIdentity(
            context,
            IdentityType.SamAccountName,
            username);
        GroupPrincipal group = GroupPrincipal.FindByIdentity(
            context, groupname);
    
        return user.IsMemberOf(group);
    }
    

    I think I will use this approach, thanks for the suggestions though however! :-)

提交回复
热议问题