我要定在Microsoft Active Directory用户的 LastPasswordSet
属性。
I want to set the LastPasswordSet
attribute of a user in Microsoft Active Directory.
在.NET UserPrincipal
API公开 LastPasswordSet
属性为只读。
The .NET UserPrincipal
API exposes the LastPasswordSet
property as readonly.
有没有解决的办法,来设置(可能使用ADSI)?值
Is there a way around this, to set the value (perhaps using ADSI)?
编辑:
:
MSDN provides the following example code:
usr.Properties["pwdLastSet"].Value = -1; // To turn on, set this value to 0.
usr.CommitChanges();
这将强制用户在下次登录时更改他们的密码。我presume如果我取代-1,日期时间,在相关格式,这将做我想做的。
This forces the user to change their password at next logon. I presume if I replace -1 with a date-time in the relevant format, this will do what I want.
这不,但是,显示我怎么弄个的主体(presumably USR
)。我会upvote任何有助于我找到了这一点。
It does not, however, show how I get hold of the principal (presumably usr
). I'll upvote anything that helps me find this out.
另一种方式是通过的 DirectorySearcher从
使用的用户登录。类
Another way would be to perform a search against the AD through the DirectorySearcher
class using the login of your users.
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
属性时,你确保用户存在并没有拼写错误。
Then, when wanting to set your PasswordLastSet
property, you assure that the user exists and that there is no spelling mistakes.
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();
}