【问题标题】:VBNet changing Active Directory passwords of expired or one time password accountsVB Net 更改过期或一次性密码帐户的 Active Directory 密码
【发布时间】:2021-04-20 12:38:23
【问题描述】:

我需要在 VB.NET 中更改 Windows 10 中 Active Directory 帐户的密码。

我编写的程序以本地管理员身份运行,

我使用有效用户帐户的工作代码是(Domain_xps 和 UserName_xps 是字符串,pwdPtr System.Runtime.InteropServices.Marshal.SecureStringToBSTR 是 SecureString):

dEntry = New DirectoryServices.DirectoryEntry("LDAP://" & Domain_xps, UserName_xps
     , System.Runtime.InteropServices.Marshal.PtrToStringBSTR(pwdPtr)
     , System.DirectoryServices.AuthenticationTypes.Secure 
       + System.DirectoryServices.AuthenticationTypes.Sealing 
       + System.DirectoryServices.AuthenticationTypes.ServerBind) ',pwd)

nativeObject = dEntry.NativeObject

Dim searcher_Fullname_xpo As System.DirectoryServices.DirectorySearcher 
                             = New System.DirectoryServices.DirectorySearcher(dEntry)    
With searcher_Fullname_xpo    
  .Filter = "(&(objectClass=User) (sAMAccountName=" & UserName_xps & "))"
End With    
result_xpo = searcher_Fullname_xpo.FindOne
Dim user As DirectoryServices.DirectoryEntry 'open directory
user = result_xpo.GetDirectoryEntry() 'get directory results
user.Username = UserName_xps
user.Password = PWD_xps    
user.Path = result_xpo.GetDirectoryEntry().Path
user.AuthenticationType = System.DirectoryServices.AuthenticationTypes.Secure 
                          + System.DirectoryServices.AuthenticationTypes.Sealing 
                          + System.DirectoryServices.AuthenticationTypes.ServerBind
user.Options.PasswordPort = 389
user.Options.PasswordEncoding = 1
user.Invoke("ChangePassword", New Object() {PWD_xps, PWDNeu_xps})
user.CommitChanges() 'commit changes
user.Close() 'close directory

但是,如果帐户在假期期间过期,或者如果生成了具有一次性密码的新用户并尝试更改其密码,则会出现错误。

用户名或密码错误。

在调试时我注意到,以下行会产生相同的错误。

nativeObject = dEntry.NativeObject

Dim searcher_Fullname_xpo As System.DirectoryServices.DirectorySearcher = New System.DirectoryServices.DirectorySearcher(dEntry)    
With searcher_Fullname_xpo    
  .Filter = "(&(objectClass=User) (sAMAccountName=" & UserName_xps & "))"
End With    
result_xpo = searcher_Fullname_xpo.FindOne

还有

user.Options.PasswordPort = 389
user.Options.PasswordEncoding = 1
user.Invoke("ChangePassword", New Object() {PWD_xps, PWDNeu_xps})

在将每一行封装在 try catch 表达式中之后,每一个都会产生错误并且不会正确更改密码。

Active Directory 显示更改,但用户帐户不再有效。

我尝试了基本相同的方法,希望我可以设置选项。

Dim ADS_OPTION_PASSWORD_PORTNUMBER As Long = 6
  Dim ADS_OPTION_PASSWORD_METHOD As Long = 7

  Dim ADS_PASSWORD_ENCODE_REQUIRE_SSL As Integer = 0
  Dim ADS_PASSWORD_ENCODE_CLEAR As Integer = 1
  Try
    user.Invoke("SetOption", New Object() {ADS_OPTION_PASSWORD_PORTNUMBER, 389})
  Catch ex3 As Exception

  End Try

  Try
    user.Invoke("SetOption", New Object() {ADS_OPTION_PASSWORD_METHOD, ADS_PASSWORD_ENCODE_CLEAR})
  Catch ex3 As Exception

  End Try

我也试过了

 user.Invoke("SetPassword", New Object() {PWDNeu_xps})

错误信息保持不变

设置密码似乎是正确的方法,但由于我无法设置密码端口或启用密码方法,它会产生相同的错误。

我还发现了这个旧线程How to change password in active directory when password expired

但这在 Windows 10 下不再可能。

如何在 VB Net 中使用所需选项更改过期帐户的密码,或者我可以配置帐户,以便实现它。

所以对这种情况进行更新。 2021 年 10 月 28 日

我还尝试了 Microsoft 或此处所述的 powershell 命令作为解决方法。

同样的事情发生了,密码更改是不可能的,

Set-ADAccountPassword : 服务器已拒绝客户端凭据。

对于有效用户,该命令的工作方式与在 DotNet 中一样

【问题讨论】:

  • 您是否有权访问代表用户的UserPrincipal 对象?如果是这样,那有一个您可以查看的 SetPassword 方法。
  • 不,我也没有,我无法取消选项,它不是我的服务器。
  • 您的代码中似乎还有一个错字:“SetPasword”
  • 是的,这是一个错字,它不会改变基本问题。

标签: vb.net active-directory ldap passwords


【解决方案1】:

在 32 位版本的 Microsoft Windows 上使用 32 位版本的 C 或最高 C++11 编写程序,最高版本为 XP pro sp2(而不是更高版本),它可以执行您想要执行的操作。使用 Code::Blocks 将其编译到 17.12 版而不是更高版本。在此过程之前或期间,请勿在该计算机上安装任何版本的 Visual C++ 或 Visual Basic 或 Visual Studio。

在您的 Windows 10 计算机上加载并运行该程序,作为 Windows 10 将“允许”的最高管理员级别。如果你不能让它编译为 GUI 运行,那么试着让它编译为 CLI 运行。请记住,这非常重要:将程序编译为不使用外部 dll 的独立单个可执行文件。

在任何 Windows 10 命令之外更改密码。使用您的程序编辑它们所在的文件,不要告诉 Windows 10 正在发生任何事情。

【讨论】:

    猜你喜欢
    • 2023-03-24
    • 2010-11-26
    • 2015-09-11
    • 1970-01-01
    • 2013-04-24
    • 2012-06-26
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多