防止Active Directory用户更改密码的方法

0 投票
1 回答
726 浏览
提问于 2025-04-15 18:50

在通过脚本创建Active Directory用户时,我还需要设置一个选项,让他们不能更改自己的密码。在管理界面上,这个操作很简单,只需勾选“用户不能更改密码”就可以了。不过,通过编程来实现就没那么容易了。我找到了一份教程,需要使用ADSI COM API,但由于一些技术原因,我希望通过.NET API来完成这个操作(简单来说,就是我无法从我的脚本访问ADSI COM API)。

我尝试将上述教程转换为纯.NET代码,如下的Python代码片段所示,但遗憾的是没有效果:

dir_entry = System.DirectoryServices.DirectoryEntry(ad_user)
obj_sec = dir_entry.ObjectSecurity
# Password GUID
guid = System.Guid(System.String("ab721a53-1e2f-11d0-9819-00aa0040529b"))
for identity in (r"NT AUTHORITY\SELF", "EVERYONE"):
    identity = System.Security.Principal.NTAccount(identity)
    access_rule = System.DirectoryServices.ActiveDirectoryAccessRule(
            identity,
            System.DirectoryServices.ActiveDirectoryRights.ExtendedRight,
            System.Security.AccessControl.AccessControlType.Deny,
            guid
            )
    obj_sec.AddAccessRule(access_rule)
dir_entry.ObjectSecurity = obj_sec
dir_entry.CommitChanges()

非常感谢任何帮助 :)

1 个回答

0

如果你能使用 .NET 3.5,那么里面有一个新的命名空间叫做 System.DirectoryServices.AccountManagement。在这个命名空间里,有一个叫 UserPrincipal 的类,它可以让你设置“无法更改密码”。你只需要把一个叫 UserCannotChangePassword 的属性设置为 false 就可以了。

撰写回答