【问题标题】:Setting the LastPasswordSet date for a user in Active Directory为 Active Directory 中的用户设置 LastPasswordSet 日期
【发布时间】:2010-05-25 13:55:38
【问题描述】:

我想在 Microsoft Active Directory 中设置用户的LastPasswordSet 属性。

.NET UserPrincipal API 将 LastPasswordSet 属性公开为只读。

有没有办法解决这个问题,设置值(可能使用 ADSI)?

编辑:

MSDN 提供以下示例代码:

usr.Properties["pwdLastSet"].Value = -1; // To turn on, set this value to 0.
usr.CommitChanges();

这会强制用户在下次登录时更改密码。我想如果我用相关格式的日期时间替换 -1,这会做我想要的。

然而,它并没有显示我是如何获得委托人的(大概是usr)。我会投票赞成任何有助于我发现这一点的内容。

【问题讨论】:

标签: .net active-directory adsi lastpasswordset


【解决方案1】:

另一种方法是使用用户的登录名通过DirectorySearcher 类对AD 执行搜索。

public DirectoryEntry GetUser(string domain, string loginName) {
    DirectorySearcher ds = new DirectorySearcher();
    ds.SearchRoot = new DirectoryEntry(domain);
    ds.SearchScope = SearchScope.Subtree;
    ds.PropertiesToLoad.Add("sAMAccountName");
    ds.PropertiesToLoad.Add("pwdLastSet");
    ds.Filter = string.Format("(&(objectCategory=person)(objectClass=user)(sAMAccountName={0})", loginName);

    SearchResult sr = null;

    try {
        sr = ds.FindOne();
        if (sr == null) return null;
        return sr.GetDirectoryEntry();
    } catch (Exception) {
        throw;
    }
}

然后,当您想要设置PasswordLastSet 属性时,请确保用户存在并且没有拼写错误。

string loginName = "AstonB1";

using(DirectoryEntry user = GetUser(loginName)) {
    if (user == null) return;

    user.Properties["pwdLastSet"].Value = whatever-format-the-date-should-be;
    user.CommitChanges();
    user.Close();
}

【讨论】:

    【解决方案2】:

    这样的?

    var usr = new DirectoryEntry("LDAP://CN=Old User,CN=users,DC=fabrikam,DC=com");
    usr.Properties["pwdLastSet"].Value = whatever-format-the-date-should-be;
    usr.CommitChanges();
    

    尚未测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-30
      • 2018-04-08
      • 1970-01-01
      • 2011-05-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多