簡體   English   中英

使用LDAP和C#設置Active Directory帳戶過期

[英]Setting Active Directory Account Expiration with LDAP and C#

我想將新用戶帳戶設置為在創建后的90天內到期。 這是我創建用戶並設置所有內容的代碼。 一切正常,除了我試圖將其設置為過期的最后一個塊。

            DirectoryEntry newUser = dirEntry.Children.Add("CN=" + cnUser, "user");
            newUser.Properties["samAccountName"].Value = cnUser;
            newUser.Properties["userPrincipalName"].Value = cnUser;
            newUser.Properties["pwdLastSet"].Value = 0;
            newUser.CommitChanges();

            //Changes Password
            String passwrd = userPassword.ToString();
            newUser.Invoke("SetPassword", new object[] { passwrd });
            newUser.CommitChanges();

            //Sets User Account to Change Passowrd on new login
            newUser.Properties["pwdLastSet"].Value = 0;
            newUser.CommitChanges();

            //Enables account
            newUser.Properties["userAccountControl"].Value = (int)newUser.Properties["userAccountControl"].Value & ~0x2;
            newUser.CommitChanges();

            //Set the account to expire in 90 days
            var dt1 = DateTime.Today.AddDays(90);
            newUser.Properties["accountExpires"].Value = dt1.ToFileTime().ToString();
            newUser.CommitChanges();

關於如何開展工作的任何建議?

謝謝

請參閱有關此字段的文檔 你需要將其轉換為“滴答聲” -

the number of 100-nanosecond intervals since January 1, 1601 (UTC). A value of 0 or 0x7FFFFFFFFFFFFFFF (9223372036854775807) indicates that the account never expires.

new DateTime(DateTime.UtcNow.AddDays(90).Ticks - new DateTime(1601, 1, 1).Ticks)將為您提供正確且准確的值。

您可以通過從上面的表達式獲取值並執行來檢查您的工作(手動):

w32tm.exe /ntte 130149277684873234

以上命令的結果對我來說是

150635 17:42:48.4873234 - 6/5/2013 12:42:48 PM

或者你可以這樣做:

DateTime expire = System.DateTime.Now.AddDays(90);
newUser.Properties["accountExpires"].Value = Convert.ToString((Int64)expire.ToFileTime());
newUser.CommitChanges();

這比處理蜱蟲和所有這些更容易處理

參考: https//msdn.microsoft.com/en-us/library/ms180914(v = vs.80).aspx

//Use the DirectoryEntry.InvokeSet method to invoke the AccountExpirationDate property setter.

System.DirectoryServices.DirectoryEntry dirEntryLocalMachine =
    new System.DirectoryServices.DirectoryEntry("WinNT://" + Environment.MachineName + "/" + userID);

dirEntryLocalMachine .InvokeSet("AccountExpirationDate", new object[] {new DateTime(2005, 12, 29)});

//Commit the changes.
usr.CommitChanges();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM