2010-02-03 89 views
0

从脚本创建Active Directory用户时,我还需要设置它们不能更改其密码的选项。通过管理GUI,可以轻松完成,方法是选中“用户无法更改密码”。然而,以编程方式,这是另一回事。我发现recipe涉及与ADSI COM API交互,但出于技术原因,我希望通过.NET API完成相同操作(简短版本:我无法通过脚本访问ADSI COM API) 。防止Active Directory用户使用DirectoryServices更改他/她的密码

我曾尝试翻译上述配方纯.NET,因为在这条巨蟒片段中可以看出,但不幸没有影响:

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

如果你可以使用.NET 3.5,那里有一个名为System.DirectoryServices.AccountManagment的新名字空间。该名称空间中的UserPrincipal类将允许您通过简单地将布尔型UserCannotChangePassword属性设置为false来设置“无法更改密码”。

+0

谢谢,看起来很有趣。我将不得不查看是否可以通过Python for .NET桥接来使用该.NET版本。 – aknuds1 2010-02-03 15:14:49

+0

非常感谢,这个解决方案非常完美! – aknuds1 2010-02-03 19:57:18

相关问题