【发布时间】:2010-06-14 13:05:47
【问题描述】:
有人可以在 windows 广告中发布知道特定用户是否是停用用户的方法吗?
【问题讨论】:
-
+1 在整个 SO 中找到与 Active Directory 相关的问题总是很有趣。 =)
标签: c# active-directory directoryservices directoryentry
有人可以在 windows 广告中发布知道特定用户是否是停用用户的方法吗?
【问题讨论】:
标签: c# active-directory directoryservices directoryentry
如果您使用 .NET 3.5 或可以升级到 .NET 3.5 - 请查看新的 System.DirectoryServices.AccountManagement 命名空间,它使许多这些操作变得轻而易举。有关介绍,请参阅 Managing Directory Security Principals in the .NET Framework 3.5。
在您的情况下,您可以编写如下代码:
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN")
UserPrincipal user = UserPrincipal.FindByIdentity("somename");
bool locked = user.IsAccountLockedOut();
就是这样! .NET 3.5 极大地改进了用户和组的大多数日常操作 - 使用这些新功能!
【讨论】:
这是一个很好的 AD 操作链接Howto: (Almost) Everything In Active Directory via C#
您需要查询 userAccountControl 属性,它是一个按位标志,我相信它是禁用帐户的 514,但这些值是累积的,因此您需要计算出来。 (NORMAL ACCOUNT + ACCOUNT DISABLED = 512 + 2 = 514).
这是所有User Account Control flags 的参考。
【讨论】:
您需要查询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 标志属性。
我想你应该参考的一个好页面如下:
How to (almost) everything in Active Directory in C#.
在与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);
}
这有帮助吗?
【讨论】: