从本地组中删除域用户 [英] Remove Domain User from Local Groups

查看:118
本文介绍了从本地组中删除域用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

帮助我正在尝试从本地组中删除域用户,并且每次尝试获取域用户的本地组时,集合都是空的.如何从计算机上的任何本地组中删除域用户.下面是我正在使用的代码,它对本地用户有效,但是如上面对域用户所述,它表示他们没有本地组.我知道域用户位于本地计算机上的用户"和"IIS_USRS"组中.

Help I am trying to remove domain users from local groups and every time I try to get the local groups for a domain user the collection is empty. How can I remove a domain user from any local groups on my machine. Below is the code I am using and it works fine for local users, but as stated above on domain users it says they have no local groups. I know for a fact that the domain user is in the Users and IIS_USRS groups on the local machine.

using (PrincipalContext localContext = new PrincipalContext(ContextType.Machine))
            {
                try
                {
                    foreach (GroupPrincipal principal in user.GetGroups(localContext))
                    {
                        principal.Members.Remove(user);
                        principal.Save(localContext);
                        principal.Dispose();
                    }
                }

推荐答案

为了使它正常工作,我不得不这样做.

In order to get this to work I ended up having to do.

using (PrincipalContext localContext = new PrincipalContext(ContextType.Machine))
            {
                try
                {
                    foreach (string g in groups)
                    {
                        using (GroupPrincipal localGroup = GroupPrincipal.FindByIdentity(localContext, IdentityType.Name, g))
                        {
                            foreach (Principal groupUser in localGroup.GetMembers().Where(groupUser => user.Name.Equals(groupUser.Name)))
                            {
                                localGroup.Members.Remove(groupUser);
                                localGroup.Save();
                            }
                        }
                    }
                }

这篇关于从本地组中删除域用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆