2012-08-16 67 views
0

我可以檢查用戶是否是域管理員通過下面的代碼行:當域控制器關閉時,我可以檢查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)

是否有能力檢查用戶是否是域管理員與關閉域控制器?

+0

您的網絡中是否有多個DC?如果您執行「無服務器」綁定,或者如果您檢查全局編錄('GC:// ....'),則單個DC故障不應導致您的調用失敗。如果你有一個DC,並且它離線 - 沒有機會再去查詢AD,對不起...... – 2012-08-16 07:03:57

+0

我正在寫一個應用程序,不知道在真實的基礎設施上有多少DC。但即使DC無法訪問,Windows也可以登錄域用戶。所以一些緩存必須存儲在本地計算機上。 – stukselbax 2012-08-16 07:19:42

回答

1

您可以在不聯繫域控制器的情況下檢查當前用戶是否是域管理員。

如果您的要求是檢查arbirary用戶是否是域管理員,我不認爲您可以在沒有域控制器的情況下執行此操作。

確實,Windows緩存登錄憑據以實現斷開連接的登錄目的。緩存存儲並在HKEY_LOCAL_MACHINE\SECURITY\Cache加密。按照設計,緩存只能由LSA進行解密。如果您發現一些其他方式來解密或查詢信息而不通過LSA,那麼這是一個安全漏洞,微軟可能會立即解決這個問題。所以,你唯一的希望就是LSA公開一個API來查詢存儲在憑證緩存中的組信息。據我所知,我沒有看到這樣的API存在。有關已記錄的LSA API,請參見here

相關問題