我正在分析和修改要将数据同步到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;
}
答案 0 :(得分:1)
来自oldvets的评论是正确的。 distinguishedName是存储在组的member
属性中的名称。移动对象时,DN会更改。
但是,当您移动用户时,aduser
对象未使用新位置进行更新。因此,当您现在尝试使用aduser
删除用户时,它会尝试删除旧的DN。那不会起作用。
您最好首先删除成员资格,然后将对象移至新OU。