【问题标题】:Server is unwilling to process the request when I try to save after remove member in Active Directory C#当我在 Active Directory C# 中删除成员后尝试保存时,服务器不愿意处理请求
【发布时间】: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


【解决方案1】:

oldvets 的评论是正确的。 distinctName 存储在组的member 属性中。当您移动对象时,DN 会发生变化。

但是,当您移动用户时,aduser 对象不会更新为新位置。因此,当您现在尝试使用 aduser 删除用户时,它正在尝试删除旧 DN。那是行不通的。

您最好先删除成员资格,然后将对象移动到新的 OU。

【讨论】:

  • oldovets,你是对的。我修改了代码,它运行良好。谢谢!! :)
猜你喜欢
  • 1970-01-01
  • 2019-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多