首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Windows AD无法从代码中重置密码

Windows AD无法从代码中重置密码
EN

Stack Overflow用户
提问于 2022-02-15 14:39:27
回答 2查看 230关注 0票数 0

从代码的重置、转码、用户口令再转述和使用相同的转码密码,再到再从其他业务登录到更高级的密码。但是AD并没有对用户进行身份认证,在AD中,我们从代码中更新userPasswordholcimIsRegistereduserAccountControl属性,以重置相应的密码。

当我们从ADSI手工重置机机AD用户密码时(右击用户->,转到复位密码,再转回转位密码,再复位→,复位密码),那么AD就是用新密码对用户进行认证。userPasswordunicodePwd属性都没有更新。

我们尝试从ADSI和代码更新unicodePwd属性,但是它不允许用户从我们观察到的代码中更新它的值

代码语言:javascript
运行
复制
[LDAP: error code 53 - 0000001F: SvcErr: DSID-031A12D2, problem 5003 (WILL_NOT_PERFORM)]

我们比较了ADSI之前和重新设置密码的用户详细信息,发现很少有细节被更新(BadLogonCount:0badPasswordTime:0badPwdCount:0lastLogoff:0lastLogon:0logonCount:0ModifiedmodifyTimeStampmsDS-User-Account-Control-ComputedPasswordExpired:falsePasswordLastSetuSNChangedwhenChanged)。

当我们试图修改usnChanged时,ADSI中的msDS-User-Account-Control-Computed发现这两个属性不能从ADSI中编辑,并且从代码中获得SchemaViolationException. For passwordExpiredbadLogonCount属性,我们在从代码和ADSI修改它时面对NoSuchAttributeException,这两个属性都丢失了。

不然我们怎么能把这事做好?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-02-15 15:03:11

若要重置密码,请更新unicodePwd属性。文献资料告诉您几个需求:

  1. 新密码必须采用特定格式:以双引号括起来,然后转换为UTF-16编码,以及
  2. 连接必须加密。

此页有一个如何用Java实现此操作的示例,我已经在下面粘贴了这个示例。它没有提到加密,但是您可以使用LDAPS (LDAPS),这可以通过使用LDAPS://而不是仅使用LDAP://来实现。这假设AD服务器为LDAPS正确设置,并且没有防火墙阻塞LDAPS端口(636)。

代码语言:javascript
运行
复制
/**
 * Update User Password in Microsoft Active Directory
 * @param username
 * @param password
 */
public void updateUserPassword(String username, String password)
{
    try
    {
        System.out.println("updating password...\n");
        String quotedPassword = "\"" + password + "\"";
        char unicodePwd[] = quotedPassword.toCharArray();
        byte pwdArray[] = new byte[unicodePwd.length * 2];
        for (int i = 0; i < unicodePwd.length; i++)
        {
        pwdArray[i * 2 + 1] = (byte) (unicodePwd[i] >>> 8);
        pwdArray[i * 2 + 0] = (byte) (unicodePwd[i] & 0xff);
        }
        System.out.print("encoded password: ");
        for (int i = 0; i < pwdArray.length; i++)
        {
        System.out.print(pwdArray[i] + " ");
        }
        System.out.println();
        ModificationItem[] mods = new ModificationItem[1];
        mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("UnicodePwd", pwdArray));
        ldapContext.modifyAttributes("cn=" + username + BASE_NAME, mods);
    }
    catch (Exception e)
    {
        System.out.println("update password error: " + e);
    }
}
票数 0
EN

Stack Overflow用户

发布于 2022-08-10 18:27:35

或者,您可以使用PowerShell重置AD密码。

此Powershell函数帮助您从域客户端计算机重置活动目录用户密码。域用户必须是域管理的成员,才能从域工作站执行密码管理。在关闭密码时,此函数还解锁用户帐户。

代码语言:javascript
运行
复制
Function Reset-ADUserPassword {
    Param  ([Parameter(Mandatory = $true, Position = 0,
            HelpMessage = 'Enter User Name')]
        [ValidateNotNull()]
        [string] $Username,
        [Parameter(Mandatory = $true, Position = 1,
            HelpMessage = 'Enter Password')]
        [ValidateNotNull()]
        [string] $Password
    )
    Invoke-Command -ComputerName <ActiveDirectoryServerName> -ArgumentList $Username, $Password  -ScriptBlock { param($Username, $Password) import-module activedirectory; Unlock-ADAccount -Identity $Username ; enable-adaccount $Username ; Set-ADAccountPassword $Username -NewPassword (ConvertTo-SecureString -AsPlainText $Password -Force); get-aduser -identity $Username -Properties * | select-object UserprincipalName }
    Write-Output "Password Set Successfully for the user: $Username and the password is: $Password ";
    
}

在Powershell上运行上述函数后,使用下面的命令重置密码。

代码语言:javascript
运行
复制
Reset-ADUserPassword -Username abc -Password test@123 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71128351

复制
相关文章

相似问题

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