userAccountControl属性如何在AD中工作? (C#)

时间:2009-07-17 18:34:07

标签: c# active-directory

userAccountControl属性如何在AD中工作?

假设我想创建一个新的用户帐户并将其设置为启用(默认情况下禁用),并将“password never expires”选项设置为true。我可以做这样的事情并且有效:

//newUser is a DirectoryEntry object
newUser.Properties["userAccountControl"].Value = 0x200; // normal account
newUser.Properties["userAccountControl"].Value = 0x10000; //password never expires

通常情况下,我认为第二行会擦除第一行,但事实并非如此。这是如何运作的?我可以将它们组合成一行吗?如果我想让密码过期,我将如何取消该值? 谢谢!

3 个答案:

答案 0 :(得分:2)

(Almost) Everything In Active Directory via C#

如何设置标志:

int val = (int)newUser.Properties["userAccountControl"].Value; 
newUser.Properties["userAccountControl"].Value = val | 0x10000; //password never expires
newUser.CommitChanges();

答案 1 :(得分:2)

你会组合标志,所以0x200 + 0x10000,这将是0x10200。有关详细信息,请参阅此文章:http://support.microsoft.com/kb/305144

答案 2 :(得分:2)

实际上,设置第二个值确实会消灭第一个点,但第一个点确实有点“不必要”......

当然,您可以将它们(实际上是多个)合并为一个值,并使用单个赋值进行设置:

const int UF_ACCOUNTDISABLE = 0x0002;
const int UF_PASSWD_NOTREQD = 0x0020;
const int UF_PASSWD_CANT_CHANGE = 0x0040;
const int UF_NORMAL_ACCOUNT = 0x0200;
const int UF_DONT_EXPIRE_PASSWD = 0x10000;
const int UF_SMARTCARD_REQUIRED = 0x40000;
const int UF_PASSWORD_EXPIRED = 0x800000;

int userControlFlags = UF_PASSWD_NOTREQD + UF_NORMAL_ACCOUNT + UF_DONT_EXPIRE_PASSWD;

newUser.Properties["userAccountControl"].Value = userControlFlags;

马克