首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为使用System.DirectoryServices.DirectoryEntry的新AD用户设置密码永不过期(ADLDS,ADAM,Active应用程序模式)

为使用System.DirectoryServices.DirectoryEntry的新AD用户设置密码永不过期(ADLDS,ADAM,Active应用程序模式)
EN

Stack Overflow用户
提问于 2020-05-28 09:34:59
回答 1查看 1.4K关注 0票数 0

我看过this question,并尝试使用"userPrincipalName“和"userFlags”,在这两种情况下,我都收到了“指定的目录服务属性或值不存在”。

我能够在Powershell中使用以下内容设置属性

代码语言:javascript
复制
Get-ADUser -Filter 'cn -like "*username*"' -Server <serveraddrress> -SearchBase "<searchbase>" | Set-ADUser -PasswordNeverExpires:$True

我的C#代码如下所示,如果删除行设置userAccountControl,它可以正常工作,但它会创建密码永不过期= FALSE的用户

代码语言:javascript
复制
using (var dirEntry = entry.Children.Add("CN=" + username, "user"))
{
    dirEntry.Properties["userPrincipalName"].Value = string.Format("{0}@principal", username);
    dirEntry.CommitChanges();
    dirEntry.Options.PasswordPort = port;
    dirEntry.Options.PasswordEncoding = PasswordEncodingMethod.PasswordEncodingClear;
    dirEntry.Invoke("SetPassword", password);

    if (!string.IsNullOrEmpty(email))
         dirEntry.Properties["mail"].Value = email;
    dirEntry.Properties["msDS-UserAccountDisabled"].Value = "FALSE";

    //this doesn't work with both "userAccountControl" and "userFlags"
    dirEntry.Properties["userAccountControl"].Value = (int)(dirEntry.Properties["userAccountControl"].Value ?? 0) | 0x10000;

    dirEntry.CommitChanges();
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-28 13:53:14

您是否使用Active应用程序模式(ADAM)?这句话让我觉得你是:

代码语言:javascript
复制
dirEntry.Properties["msDS-UserAccountDisabled"].Value = "FALSE";

该属性在普通中不存在。所以,如果这对你有用,那你就是在利用亚当。

如果使用的是ADAM,则使用msDS-UserDontExpirePassword属性使密码不过期:

代码语言:javascript
复制
dirEntry.Properties["msDS-UserDontExpirePassword"].Value = true;

如果您不使用ADAM,那么"msDS-UserAccountDisabled就不存在,也不应该使用它。相反,使用userAccountControl来设置两者。

因为您刚刚创建了这个帐户,您不需要担心已经存在的值,您只需将其设置为特定的值即可。假设您希望使用“不要过期密码”标志启用它,那么您可以使用以下命令:

代码语言:javascript
复制
//NORMAL_ACCOUNT | DONT_EXPIRE_PASSWORD
dirEntry.Properties["userAccountControl"].Value = 0x0200 | 0x10000;

您可以将每个值分开,或者只使用产生的66048十进制值。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62061447

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档