我有一个ADUsers的列表,但在每个ADUSER中,我想要获取它的属性"LastPasswordSet“,它只能通过UserPrincipal访问(不确定是否还有其他方法)。
如果我使用这个代码,
PrincipalContext l_objContext = new PrincipalContext(ContextType.Domain, l_strDomain, l_strUserOU);
foreach (ADUser ADuser in Users)
{
UserPrincipal usr = UserPrincipal.FindByIdentity(l_objContext, ADuser.CommonName.ToString());
if (usr.LastPasswordSet.HasValue)
{
EmailString(usr.LastPasswordSet.ToString());
}
}现在我不想给UserPrincipal提供任何东西,除了ADUSER的任何属性,上面的代码也不起作用,问题是它发送了几封电子邮件,然后在它给出的某个地方遇到错误和服务停止,这可能是因为一些无效的日期(我不想修复它,只是为了让你知道我在做什么)
发布于 2012-03-19 21:26:31
您可以创建自己的PrincipalContext,然后使用UserPrincipal.FindByIdentity获取主体。从这里开始,我想您已经知道了,您可以调用LastPasswordSet属性。
public static DateTime? GetLastPasswordSet(string domain, string userName)
{
using (var context = new PrincipalContext(ContextType.Domain, domain))
{
var userPrincipal = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, userName);
return userPrincipal.LastPasswordSet;
}
}注意:您需要添加一个对System.DirectoryServices.AccountManagement的引用
编辑:在评论中回答进一步的问题
要测试密码是否最后一次设置是在一个多月前-如下所示:
if (lastPasswordSet < DateTime.Now.AddMonths(-1))
{
// Password last set over a month ago.
}要记住的一件事是,一个月是一个模棱两可的时间长度-它的长度取决于你所在的月份(和年份)。根据您正在尝试做的事情,可能更适合有一个固定的时间段,如28天。
https://stackoverflow.com/questions/9770877
复制相似问题