C# userAccountControl 属性如何在 AD 中工作?(C#)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1144966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does the userAccountControl property work in AD? (C#)
提问by Jason
How does the userAccountControl property work in AD?
userAccountControl 属性如何在 AD 中工作?
Let's say I want to create a new user account and set it to enabled (it's disable by default), and also set the 'password never expires' option to true. I can do something like this and it works:
假设我想创建一个新用户帐户并将其设置为启用(默认情况下禁用),并将“密码永不过期”选项设置为 true。我可以做这样的事情,它的工作原理:
//newUser is a DirectoryEntry object
newUser.Properties["userAccountControl"].Value = 0x200; // normal account
newUser.Properties["userAccountControl"].Value = 0x10000; //password never expires
Normally, I would think the second line would wipe the first one out, but it doesn't. How does that work? Can I combine them in one line? How would I then take away that value if I wanted to have their password expire? Thanks!
通常,我会认为第二行会清除第一行,但事实并非如此。这是如何运作的?我可以将它们合并为一行吗?如果我想让他们的密码过期,我将如何取消该值?谢谢!
采纳答案by marc_s
Actually, setting the second value will indeed wipe out the first - point is though, the first is really a bit "unnecessary".....
实际上,设置第二个值确实会消除第一个值-尽管如此,第一个值确实有点“不必要”.....
And of course you can combine them (and multiple ones, really) into a single value and set it with a single assignment:
当然,你可以将它们(和多个,真的)组合成一个值,并用一个赋值来设置它:
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;
Marc
马克
回答by Fry
(Almost) Everything In Active Directory via C#
(几乎)通过 C# 在 Active Directory 中的所有内容
How to set a flag:
如何设置标志:
int val = (int)newUser.Properties["userAccountControl"].Value;
newUser.Properties["userAccountControl"].Value = val | 0x10000; //password never expires
newUser.CommitChanges();
回答by Michael Morton
You would combine the flags, so 0x200 + 0x10000, which would be 0x10200. See this article for more information: http://support.microsoft.com/kb/305144.
您将组合标志,因此 0x200 + 0x10000,即 0x10200。有关详细信息,请参阅此文章:http: //support.microsoft.com/kb/305144。