2012-03-19 19 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

相關問題