我正在写一些微型AD工具(与VS-C#)到我们的组织,并陷入了一个问题。C#问题与操纵ActiveDirectory用户
我有一个主要功能,搜索用户(当我点击它在一个列表视图)和一些功能,操纵用户的对象。
public DirectoryEntry GetUser(string username)
{
try
{
Forest currentForest = Forest.GetCurrentForest();
GlobalCatalog gc = currentForest.FindGlobalCatalog();
using (DirectorySearcher searcher = gc.GetDirectorySearcher())
{
searcher.Filter = "(&((&(objectCategory=Person)(objectClass=User)))(samaccountname=" + username + "*))";
SearchResult results = searcher.FindOne();
if (!(results == null))
{
DirectoryEntry de = new DirectoryEntry(results.Path, strAdminUser, strAdminPass, AuthenticationTypes.Secure);
de.RefreshCache(new string[] { "canonicalName" });
de.Path = de.Properties["canonicalName"].Value.ToString();
de.CommitChanges();
return de;
}
else
{
return null;
}
}
}
catch (DirectoryServicesCOMException e)
{
System.Windows.Forms.MessageBox.Show(e.Message);
return null;
}
}
,这里是一个检查,如果用户被锁定的功能的例子:
public bool IsUserLocked(string username)
{
try
{
DirectoryEntry de = GetUser(username);
string attName = "msDS-User-Account-Control-Computed";
de.RefreshCache(new string[] { attName });
const int UF_LOCKOUT = 0x0010;
int userFlags = /*(int)*/Convert.ToInt32(de.Properties[attName].Value);
if ((userFlags & UF_LOCKOUT) == UF_LOCKOUT)
{
return true;
}
de.Dispose();
return false;
}
catch (DirectoryServicesCOMException e)
{
System.Windows.Forms.MessageBox.Show(e.Message);
return false;
}
}
,检查用户的锁定状态总是失败,出现错误的功能:“未指定的错误”,但如果我没有更改第一个函数中的目录项的路径,我得到“服务器不愿意处理请求”错误(我正在使用正确的服务用户名和密码以及所需的所有权限),但仍然它发生。
有人可以发现问题吗?
做一个谷歌搜索..有几个例子在网上以及在Stackoverflow关于检查锁定状态使用C#在这里检查初学者 - http://stackoverflow.com/questions/2005637/如何确定,如果用户帐户是启用或禁用的 – MethodMan