关于域用户删除的的问题?

gzw19830619 2009-12-04 09:49:41

//创建新的用户 默认Users下
string newUsername = this.TextBox1.Text.Trim();

try
{
DirectoryEntry ent = new DirectoryEntry();
DirectoryEntry ou = ent.Children.Find("CN=Users");
DirectoryEntry usr = ou.Children.Add("CN=" + newUsername, "user");
usr.Properties["samAccountName"].Value = newUsername;
//usr.Invoke("SetPassword", new Object[] { passWord });
usr.CommitChanges();
usr.Invoke("SetPassword", new Object[] { passWord });
int val = (int)usr.Properties["userAccountControl"].Value;
usr.Properties["userAccountControl"].Value = val &
~ADS_UF_ACCOUNTDISABLE;
usr.CommitChanges();
}
catch (System.Exception ex)
{
//异常的场合
System.Windows.Forms.MessageBox.Show(ex.Message);
return;
}

//读取指定组
public DirectoryEntry GetDirectoryEntryOfGroup(string groupName)
{
DirectoryEntry de = new DirectoryEntry();
DirectorySearcher deSearch = new DirectorySearcher(de);
deSearch.Filter = "(&(objectClass=group)(cn=" + groupName + "))";
deSearch.SearchScope = SearchScope.Subtree;

try
{
SearchResult result = deSearch.FindOne();
de = new DirectoryEntry(result.Path);
return de;
}
catch
{
return null;
}
}

//读取指定用户
public DirectoryEntry GetDirectoryEntry(string commonName)
{
DirectoryEntry de = GetDirectoryObject();
DirectorySearcher deSearch = new DirectorySearcher(de);
deSearch.Filter = "(&(&(objectCategory=person)(objectClass=user))(cn=" + commonName + "))";
deSearch.SearchScope = SearchScope.Subtree;

try
{
SearchResult result = deSearch.FindOne();
de = new DirectoryEntry(result.Path);
return de;
}
catch
{
return null;
}
}

//从指定组中删除指定用户
DirectoryEntry group = this.GetDirectoryEntryOfGroup("Users");
DirectoryEntry entry = this.GetDirectoryEntry(this.TextBox1.Text.Trim());
group.Properties["member"].Remove(entry.Properties["distinguishedName"].Value);
group.CommitChanges();//执行到这时报“该服务器不愿意处理该请求。 (异常来自 HRESULT:0x80072035)”

哪位大侠知道是怎么回事啊,谢谢!
我现在的登录用户已经是Administrator管理员用户了
...全文
173 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
cyliner 2009-12-04
  • 打赏
  • 举报
回复
直接删除用户就行了,不需再去查找组

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧