C# 当域控制器关闭时,我可以检查AD用户是否是域管理员吗?

C# 当域控制器关闭时,我可以检查AD用户是否是域管理员吗?,c#,active-directory,domaincontroller,C#,Active Directory,Domaincontroller,我可以通过以下代码行检查用户是否是域管理员: using (DirectoryEntry domainEntry = new DirectoryEntry(string.Format("LDAP://{0}", domain))) { byte[] domainSIdArray = (byte[])domainEntry.Properties["objectSid"].Value; SecurityIdentifier domainSId = new SecurityIdent

我可以通过以下代码行检查用户是否是域管理员:

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);
    }
}
使用(DirectoryEntry domainEntry=newdirectoryEntry(string.Format(“LDAP://{0}”,domain)))
{
字节[]domainSIdArray=(字节[])domainEntry.Properties[“objectSid”].Value;
SecurityIdentifier domainSId=新的SecurityIdentifier(domainSIdArray,0);
SecurityIdentifier DomainAdminsId=新的SecurityIdentifier(WellKnownSidType.AccountDomainAdminsId,domainSId);
使用(DirectoryEntry groupEntry=newdirectoryEntry(string.Format(“LDAP://”),BuildOctetString(domainAdminsId)))
{
字符串adminDn=groupEntry.Properties[“DifferentizedName”]。值为字符串;
SearchResult=(新的DirectorySearcher(domainEntry,string.Format(&(objectCategory=user)(samAccountName={0})),userName),new[]{“memberOf”})).FindOne();
返回结果.Properties[“memberOf”]包含(adminDn);
}
}
更多细节

但当域控制器关闭或离线(没有任何连接)时,我得到以下错误:

服务器无法运行。

位于System.DirectoryServices.DirectoryEntry.Bind(布尔throwIfFail)
位于System.DirectoryServices.DirectoryEntry.Bind()
在System.DirectoryServices.DirectoryEntry.get_AdsObject()中 位于System.DirectoryServices.PropertyValueCollection.PopulateList()
位于System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry 条目,字符串属性名称)
位于System.DirectoryServices.PropertyCollection.get_项(字符串propertyName)


是否能够在域控制器关闭的情况下检查用户是否为域管理员?

您可以在不联系域控制器的情况下检查当前用户是否为域管理员

如果您的要求是检查任意用户是否是域管理员,我认为如果没有域控制器,您就无法做到这一点


确实,Windows缓存登录凭据以用于断开连接的登录目的。缓存在
HKEY\U LOCAL\U MACHINE\SECURITY\cache
中存储和加密。根据设计,缓存只能由LSA描述。如果您找到其他方法来解密或查询信息,而不必通过LSA,这是一个安全漏洞,Microsoft可能会立即修复它。因此,您唯一的希望就是LSA以某种方式公开了一个API来查询存储在凭证缓存中的组信息。据我所知,我没有看到这样的API存在。有关记录在案的LSA API,请参阅。

您的网络中是否有多个DC?如果执行“无服务器”绑定,或者检查全局编录(
GC://…
),则单个DC故障不应导致调用失败。如果你有一个DC,它是离线的-没有机会再查询广告了,对不起…我正在写一个应用程序,不知道实际基础设施上会有多少个DC。但即使DC不可访问,Windows也可以登录域用户。因此,一些缓存必须存储在本地计算机上。