6

在我的应用程序中,我正在做的事情是用户可以从我的应用程序控制他/她的本地 Windows 用户帐户,即创建用户、设置/删除密码、更改密码以及可以从我的应用程序调用密码过期策略。现在,在这一点上,我需要弄清楚如果用户想在下次登录时更改他的密码,那么会发生什么。正如许多论坛和博客所说的那样,我做了相应的编码:

下次登录时调用密码过期

 public bool InvokePasswordExpiredPolicy()
    {
        try
        {
            string path = GetDirectoryPath();
            string attribute = "PasswordExpired";
            DirectoryEntry de = new DirectoryEntry(path);
            de.RefreshCache(new string[] { attribute });
            if(de.Properties.Contains("PasswordExpired"))
            de.Properties[attribute].Value = 1;
            de.CommitChanges();
            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }

下次登录时触发密码过期。重置标志

public bool ProvokePasswordExpiredPolicy()
    {
        try
        {
            string path = GetDirectoryPath();
            string attribute = "PasswordExpired";
            DirectoryEntry de = new DirectoryEntry(path);
            de.RefreshCache(new string[] { attribute });
            de.Properties[attribute].Value = -1;
            de.CommitChanges();
            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }

检查是否设置了相关标志

public bool isPasswordPolicyInvoked()
    {
        try
        {
            string path = GetDirectoryPath();
            string attribute = "PasswordExpired";
            DirectoryEntry de = new DirectoryEntry(path);
            de.RefreshCache(new string[] { attribute });
            int value = Convert.ToInt32(de.Properties[attribute].Value);

            if (value == 1)
                return true;
            else
                return false;
        }
        catch (Exception)
        {
            return false;
        }
    }

我使用 WinNT 来获取目录路径而不是 LDAP。我使用以下方法获取目录路径。

private String GetDirectoryPath()
    {
        String uName = this.userName;
        String mName = this.userMachine;

        String directoryPath = "WinNT://" + mName + "/" + uName;

        return directoryPath;
    }

有什么我想念的吗?帮帮我。

注意:首先,我使用pwdLastSet属性设置为 0(表示开启)和 -1(表示关闭)抛出异常“在属性缓存中找不到目录属性”,后来我发现 WinNT 不支持这个属性而是它支持PasswordExpired,它需要为 1 才能设置标志。这就是我所做的。

4

4 回答 4

6

如何改用 System.DirectoryServices.AccountManagement,在这种情况下,您可以调用以下代码:

UserPrincipal.Current.ExpirePasswordNow();
于 2012-06-27T02:00:03.083 回答
6

下面的代码应该可以工作:

de.Properties["pwdLastSet"][0] = 0;

来自用户下次登录时必须更改密码(LDAP 提供者)

要强制用户在下次登录时更改密码,请将 pwdLastSet 属性设置为零 (0)。要删除此要求,请将 pwdLastSet 属性设置为 -1。pwdLastSet 属性不能设置为系统以外的任何其他值。

于 2018-02-20T13:28:46.170 回答
0

我追踪到了这一点。这对应于UserPrincipal.LastPasswordSet属性。

该属性是只读的,因此您必须使用以下方法进行设置:

    public bool UserMustChangePasswordNextLogon
    {
        get
        {
            return (_userPrincipal.LastPasswordSet == null);
        }
        set
        {
            if (value)
                _userPrincipal.ExpirePasswordNow();
            else
                _userPrincipal.RefreshExpiredPassword();
        }
    }

在我的情况下,我将变量设置为在下次保存时过期或刷新密码,而不是在属性设置器中。

于 2018-12-19T15:29:11.520 回答
-3
de.Properties["passwordExpired"][0] = 1; <br>
de.CommitChanges();
于 2013-01-09T06:29:08.627 回答