2010-05-25 25 views
1

我想在Microsoft Active Directory中設置用戶的LastPasswordSet屬性。爲Active Directory中的用戶設置LastPasswordSet日期

.NET UserPrincipal API將LastPasswordSet屬性作爲只讀屬性公開。

有沒有辦法解決這個問題,設置值(也許使用ADSI)?

編輯:

MSDN提供了以下示例代碼:

usr.Properties["pwdLastSet"].Value = -1; // To turn on, set this value to 0. 
usr.CommitChanges(); 

這迫使用戶在下次登錄時更改他們的密碼。我認爲,如果我用相關格式的日期時間替換-1,這將做我想要的。

但是,它並沒有顯示我如何得到委託人(大概usr)。我會鼓勵任何能幫助我找到答案的東西。

+0

http://blogs.technet.com/b/heyscriptingguy/archive/2005/07/06/how-can-i-cause-a-user-s-password-to-expire.aspx - 可能是有用的 – Ben 2010-05-25 14:00:39

+0

http://msdn.microsoft.com/en-gb/library/ms180915.aspx - 也可能是有用的 – Ben 2010-05-25 14:11:24

+1

好吧,我現在明白你的意圖了。您應該發佈第二個鏈接的相關部分作爲答案,以便我可以放棄它,並且您可以接受它。假裝我什麼也沒說...... ;-) – 2010-05-25 14:22:11

回答

1

另一種方法是使用用戶的登錄名通過DirectorySearcher類對AD進行搜索。

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屬性時,您確保用戶存在和不存在拼寫錯誤。

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(); 
} 
0

這樣的事情?

var usr = new DirectoryEntry("LDAP://CN=Old User,CN=users,DC=fabrikam,DC=com"); 
usr.Properties["pwdLastSet"].Value = whatever-format-the-date-should-be; 
usr.CommitChanges(); 

尚未測試。

相關問題