我可以檢查用戶是否是域管理員通過下面的代碼行:當域控制器關閉時,我可以檢查AD用戶是否是域管理員?
using (DirectoryEntry domainEntry = new DirectoryEntry(string.Format("LDAP://{0}", domain)))
{
byte[] domainSIdArray = (byte[])domainEntry.Properties["objectSid"].Value;
SecurityIdentifier domainSId = new SecurityIdentifier(domainSIdArray, 0);
SecurityIdentifier domainAdminsSId = new SecurityIdentifier(WellKnownSidType.AccountDomainAdminsSid, domainSId);
using (DirectoryEntry groupEntry = new DirectoryEntry(string.Format("LDAP://<SID={0}>", BuildOctetString(domainAdminsSId))))
{
string adminDn = groupEntry.Properties["distinguishedname"].Value as string;
SearchResult result = (new DirectorySearcher(domainEntry, string.Format("(&(objectCategory=user)(samAccountName={0}))", userName), new[] { "memberOf" })).FindOne();
return result.Properties["memberOf"].Contains(adminDn);
}
}
更多細節here
但當域控制器被關閉,或它的脫線(沒有任何連接) ,我得到以下錯誤:
The server is not operational.
at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
at System.DirectoryServices.DirectoryEntry.Bind()
at System.DirectoryServices.DirectoryEntry.get_AdsObject()
at System.DirectoryServices.PropertyValueCollection.PopulateList()
at System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName)
at System.DirectoryServices.PropertyCollection.get_Item(String propertyName)
是否有能力檢查用戶是否是域管理員與關閉域控制器?
您的網絡中是否有多個DC?如果您執行「無服務器」綁定,或者如果您檢查全局編錄('GC:// ....'),則單個DC故障不應導致您的調用失敗。如果你有一個DC,並且它離線 - 沒有機會再去查詢AD,對不起...... – 2012-08-16 07:03:57
我正在寫一個應用程序,不知道在真實的基礎設施上有多少DC。但即使DC無法訪問,Windows也可以登錄域用戶。所以一些緩存必須存儲在本地計算機上。 – stukselbax 2012-08-16 07:19:42