2012-08-16 65 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

相关问题