关于域用户删除的的问题?
//创建新的用户 默认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管理员用户了