有人可以发布知道特定用户是否是 Windows 广告中已停用用户的方法吗?
3 回答
如果您使用 .NET 3.5 或可以升级到 .NET 3.5 - 请查看新的System.DirectoryServices.AccountManagement
命名空间,它使许多这些操作变得轻而易举。有关介绍,请参阅在 .NET Framework 3.5 中管理目录安全主体。
在您的情况下,您可以编写如下代码:
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN")
UserPrincipal user = UserPrincipal.FindByIdentity("somename");
bool locked = user.IsAccountLockedOut();
这就是全部!.NET 3.5 极大地改进了用户和组的大多数日常操作 - 使用这些新功能!
您需要查询该userAccountControl
属性。
userAccountControl
标志的值是:
CONST HEX
-------------------------------
SCRIPT 0x0001
ACCOUNTDISABLE 0x0002
HOMEDIR_REQUIRED 0x0008
LOCKOUT 0x0010
PASSWD_NOTREQD 0x0020
PASSWD_CANT_CHANGE 0x0040
ENCRYPTED_TEXT_PWD_ALLOWED 0x0080
TEMP_DUPLICATE_ACCOUNT 0x0100
NORMAL_ACCOUNT 0x0200
INTERDOMAIN_TRUST_ACCOUNT 0x0800
WORKSTATION_TRUST_ACCOUNT 0x1000
SERVER_TRUST_ACCOUNT 0x2000
DONT_EXPIRE_PASSWORD 0x10000
MNS_LOGON_ACCOUNT 0x20000
SMARTCARD_REQUIRED 0x40000
TRUSTED_FOR_DELEGATION 0x80000
NOT_DELEGATED 0x100000
USE_DES_KEY_ONLY 0x200000
DONT_REQ_PREAUTH 0x400000
PASSWORD_EXPIRED 0x800000
TRUSTED_TO_AUTH_FOR_DELEGATION 0x1000000
您需要使用System.DirectoryServices
命名空间并使用DirectorySearcher
该类来查询 Active Directory,然后验证userAccountControl
标志属性。
我想您应该参考的一个好页面如下:
如何(几乎)在 C# 中的 Active Directory 中的所有内容。
与 flags 属性进行比较时,您必须按位进行userAccountControl
,如下所示:
using (DirectorySearcher searcher = new DirectorySearcher()) {
searcher.SearchRoot = new DirectoryEntry(rootDSE); // Where rootDSE is a string which contains your LDAP path to your domain.
searcher.SearchScope = SearchScope.Subtree;
searcher.Filter = string.Format("(&(objectClass=user)(sAMAccountName={0}))", userName);
SearchResult result = null;
try {
result = searcher.FindOne();
} catch (Exception) {
// You know what to do here... =P
}
if (result == null)
return;
DirectoryEntry user = result.GetDirectoryEntry();
bool isAccountDisabled = ((user.Properties("userAccountControl").Value & ACCOUNTDISABLE) == ACCOUNTDISABLE);
}
这有帮助吗?
这是 AD 操作Howto 的一个很好的链接:(几乎)通过 C# 在 Active Directory 中的所有内容
您需要查询 userAccountControl 属性,它是一个按位标志,我相信它是禁用帐户的 514,但这些值是累积的,因此您需要解决它。(NORMAL ACCOUNT + ACCOUNT DISABLED = 512 + 2 = 514)
.
这是所有用户帐户控制标志的参考。