2012-03-19 45 views
0

我有一个ADUser的列表,但foreach ADUSER我想通过UserPrincipal获取其属性“LastPasswordSet”,它只能访问(不知道是否有其他方式)。从ADUser获取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便有的属性,上面的代码也不管用,问题是,它的发送几封电子邮件,然后遇到它给出的地方,并出现错误和服务停止,这可能是因为一些无效的日期(我不想修复它,只是说,让你知道我在做什么)

+0

这似乎是你的问题是类似于这一个 - http://stackoverflow.com/questions/2905277/setting-the-lastpasswordset-date-for-a-user-in-active-directory – 2012-03-19 13:19:06

+0

我检查了这个问题已经,但他们正在改变它的价值使用UserPrincipal无论如何,如果你再次阅读我的问题,它说我如何通过使用ADUser属性获得userPrincipal .... – 2012-03-19 13:21:04

回答

2

你可以创建自己的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天。

+0

我该如何比较此属性与当前日期,看看它是否旧那么从今天的日期起一个月 – 2012-03-19 13:38:38

相关问题