我正在寫一些微型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