我看过this question,并尝试使用"userPrincipalName“和"userFlags”,在这两种情况下,我都收到了“指定的目录服务属性或值不存在”。
我能够在Powershell中使用以下内容设置属性
Get-ADUser -Filter 'cn -like "*username*"' -Server <serveraddrress> -SearchBase "<searchbase>" | Set-ADUser -PasswordNeverExpires:$True我的C#代码如下所示,如果删除行设置userAccountControl,它可以正常工作,但它会创建密码永不过期= FALSE的用户
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();
}发布于 2020-05-28 13:53:14
您是否使用Active应用程序模式(ADAM)?这句话让我觉得你是:
dirEntry.Properties["msDS-UserAccountDisabled"].Value = "FALSE";该属性在普通中不存在。所以,如果这对你有用,那你就是在利用亚当。
如果使用的是ADAM,则使用msDS-UserDontExpirePassword属性使密码不过期:
dirEntry.Properties["msDS-UserDontExpirePassword"].Value = true;如果您不使用ADAM,那么"msDS-UserAccountDisabled就不存在,也不应该使用它。相反,使用userAccountControl来设置两者。
因为您刚刚创建了这个帐户,您不需要担心已经存在的值,您只需将其设置为特定的值即可。假设您希望使用“不要过期密码”标志启用它,那么您可以使用以下命令:
//NORMAL_ACCOUNT | DONT_EXPIRE_PASSWORD
dirEntry.Properties["userAccountControl"].Value = 0x0200 | 0x10000;您可以将每个值分开,或者只使用产生的66048十进制值。
https://stackoverflow.com/questions/62061447
复制相似问题