【发布时间】:2017-04-04 04:41:01
【问题描述】:
我正在分析和修改一个即将将数据同步到 Active Directory 的 Windows 应用程序。
当我将用户移动到活动目录中的另一个部门时,
我尝试删除之前部门的成员。
Member.Remove 很好,但是当我尝试保存它时,它会抛出这样的异常
Server is unwilling to process the request
所以,什么都没有改变。可悲的是,我是 Active Directory 的新手,我不知道如何处理它。
代码如下。请分享您的知识。
void MoveUser(string ppk, string pk)
{
var aduser = adm.GetUser(pk);
var adde=aduser.GetUnderlyingObject() as DirectoryEntry;
var pde = adm.FindOU(ppk);
if (aduser == null || pde == null)
{
return;
}
adde.MoveTo(pde);
var pgroup = adm.GetGroup(ppk);
if (!aduser.IsMemberOf(pgroup))
{
var allgroups = adm.GetAllDE(Words.Group);
foreach (var sg in allgroups)
{
var samname = GetSamName(sg);
var sgroup = adm.GetGroup(samname);
if (aduser.IsMemberOf(sgroup))
{
sgroup.Members.Remove(aduser);
//exception here
//message: Server is unwilling to process the request
sgroup.Save();
}
}
pgroup.Members.Add(aduser);
pgroup.Save();
}
}
public UserPrincipal GetUser(string sUserName)
{
PrincipalContext oPrincipalContext = GetPrincipalContext();
UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(oPrincipalContext, sUserName);
return oUserPrincipal;
}
public DirectoryEntry FindOU(string ouName)
{
DirectorySearcher ds = new DirectorySearcher(GetRootOu());
ds.Filter = "(ou=" + ouName + ")";
try
{
return ds.FindOne().GetDirectoryEntry();
}
catch (Exception)
{
return null;
}
}
public GroupPrincipal GetGroup(string sGroupName)
{
PrincipalContext oPrincipalContext = GetPrincipalContext();
GroupPrincipal oGroupPrincipal = GroupPrincipal.FindByIdentity(oPrincipalContext, sGroupName);
return oGroupPrincipal;
}
【问题讨论】:
-
如果您尝试从组中删除用户,然后将其移动到新的 OU,该怎么办。 LDAP 级别的组成员身份基于专有名称,并且您更改用户 DN。另一种选择是查询用户,更改 DN,然后再次重新查询 userprincipal
-
是的,您的方法是正确的。我像您的流程一样更改了方法,它有效。感谢您的详细解释!
标签: c# active-directory userprincipal groupprincipal