我可以检查用户是否是域管理员通过下面的代码行:当域控制器关闭时,我可以检查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