我尝试用LastPasswordSet获取C#本地帐户的"toto“的日期时间
这个属性值总是变化的(毫秒或秒)。
你有什么办法修好它吗?你也有同样的行为吗?
for (int i = 0; i < 100; i++)
{
PrincipalContext context = new PrincipalContext(ContextType.Machine);
UserPrincipal user = new UserPrincipal(context) { SamAccountName = "toto" };
user = (UserPrincipal)new PrincipalSearcher(user).FindOne();
Debug.WriteLine("User : " + user.LastPasswordSet.Value.ToString("dd/MM/yyyy HH:mm:ss.fff"));
}User : 01/06/2020 09:09:34.475
User : 01/06/2020 09:09:34.479
User : 01/06/2020 09:09:34.484
User : 01/06/2020 09:09:34.489
User : 01/06/2020 09:09:34.494
User : 01/06/2020 09:09:34.499
User : 01/06/2020 09:09:34.504
User : 01/06/2020 09:09:34.509
User : 01/06/2020 09:09:34.513
User : 01/06/2020 09:09:34.519
User : 01/06/2020 09:09:34.524
User : 01/06/2020 09:09:34.528
User : 01/06/2020 09:09:34.533
User : 01/06/2020 09:09:34.538
User : 01/06/2020 09:09:34.542
User : 01/06/2020 09:09:34.547
User : 01/06/2020 09:09:34.552
User : 01/06/2020 09:09:34.557谢谢你的回答。
如何计算PasswordAge?
我想使用LastPasswordSet来确定密码是否已经更改,但是这些数据不是固定的,而且比较也不安全。以毫秒为单位,有时第二毫秒就会转到下一毫秒。
奇怪的是,LastPasswordSet不是用来计算PasswordAge的数据,反之亦然。
你好,我尝试了这段代码,但是属性LastPasswordSet不存在
var user = new DirectoryEntry("WinNT://./toto");
var lastSet = user.LastPasswordSet.Value;
var lastSetNoMilliseconds = new DateTime(lastSet.Year, lastSet.Month, lastSet.Day, lastSet.Hour, lastSet.Minute, lastSet.Second, lastSet.Kind);发布于 2020-06-02 14:30:41
Windows实际上不会公开任何告诉您密码是何时设置的属性。它确实公开了一个名为PasswordAge的属性,您可以看到如果您使用DirectoryEntry (这是UserPrincipal在后台使用的):
var user = new DirectoryEntry("WinNT://./toto");
Debug.WriteLine($"PasswordAge: {user.Properties["PasswordAge"][0]}");该PasswordAge值表示自上次更改密码以来已过的秒数。因此,如果要将其转换为更改密码的实际时间,则必须从当前时间减去该值,这是UserPrincipal在访问LastPasswordSet属性时所做的事情,如您在他们的代码中所看到的。
value = DateTime.UtcNow - new TimeSpan(0, 0, secondsLapsed);但是由于PasswordAge只有秒,这意味着您得到的毫秒值来自当前时间。
所以没什么不对的。只是不要看毫秒值。
Update:如果要存储此值以便以后进行比较,则可以在不使用毫秒的情况下复制日期:
var lastSet = user.LastPasswordSet.Value;
var lastSetNoMilliseconds = new DateTime(lastSet.Year, lastSet.Month, lastSet.Day, lastSet.Hour, lastSet.Minute, lastSet.Second, lastSet.Kind);https://stackoverflow.com/questions/62153372
复制相似问题