我想在中设置用户的LastPasswordSet属性。
.NET UserPrincipal API将LastPasswordSet属性公开为只读。
有什么方法可以绕过这个问题,来设置值(也许使用ADSI)?
编辑:
MSDN提供了以下示例代码:
usr.Properties["pwdLastSet"].Value = -1; // To turn on, set this value to 0.
usr.CommitChanges();这迫使用户在下次登录时更改密码。我想,如果我用相关格式的日期时间替换-1,这将做我想做的事情。
然而,它并没有说明我是如何获得主体(大概是usr)的。任何能帮我找到这件事的我都会投赞成票。
发布于 2010-05-25 15:18:27
另一种方法是使用用户的登录名,通过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();
}发布于 2010-05-25 14:50:31
像这样吗?
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();还没有经过测试。
https://stackoverflow.com/questions/2905277
复制相似问题