我正在分析和修改一个将数据同步到 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;
}