[英]Active Directoy LDAP - Lock User Account
鎖定Active Directory帳戶的首選方法是什么?
int val = (int)directoryentry.Properties["userAccountControl"].Value;
directoryentry.Properties["userAccountControl"].Value = val | 0x0010;
與
directoryentry.InvokeSet("IsAccountLocked", true);
有沒有更好的辦法?
實際上,您必須執行按位操作才能將正確的位設置為適當的值。 在下面的鏈接中,您將遇到用戶帳戶控制標志。 因此,您只需對該屬性執行適當的邏輯操作即可鎖定或解鎖該帳戶。
我想,以下鏈接會引起您的興趣。
我稍后會添加一個示例代碼C#代碼。
這是建議的代碼:
public class AdUser {
private int _userAccountControl
public bool IsLocked {
get {
return _userAccountControl & UserAccountControls.Lock
} set {
if(value)
_userAccountControl = _userAccountControl | UserAccountControls.Lock
else
// Must reverse all the bits in the filter when performing an And operation
_userAccountControl = _userAccountControl & ~UserAccountControls.Lock
}
}
public enum UserAccountControls {
Lock = 0x10
}
}
請考慮對此代碼進行一些更改,因為我還沒有測試過。 但是您的代碼應該與鎖定和解鎖用戶帳戶相似或類似。 遲早,您必須使用DirectoryEntry.Properties []將其設置為對象類中的值。
編輯
鎖定Active Directory帳戶的首選方法是什么?
int val = (int)directoryentry.Properties["userAccountControl"].Value; directoryentry.Properties["userAccountControl"].Value = val | 0x0010;
與
directoryentry.InvokeSet("IsAccountLocked", true);
在回答你提出的問題時,我會說這些是最簡單的方法,至少我知道。 就我而言,我更喜歡將這些功能包裝在我的代碼示例中,因為其他程序員不必關心按位操作等等。 對他們來說,他們正在操縱物體。
至於這兩者之間的最佳方式,我想這主要是一個偏好問題。 如果您對邏輯操作感到放心,這些通常是首選。 相比之下,第二種選擇更容易使用。
你是在.NET 3.5(或者你可以升級到它)?
如果是這樣,請查看新的System.DirectoryServices.AccountManagement
命名空間及其提供的所有內容! 優秀的介紹是MSDN文章管理.NET Framework 3.5中的目錄安全主體 。
對於您的情況,您必須以某種方式獲得UserPrincipal
,例如
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN");
UserPrincipal me = UserPrincipal.Current;
然后你可以訪問過多的非常容易使用的屬性和方法 - 例如:
bool isLockedOut = me.IsAccountLockedOut();
您可以使用以下方法解鎖鎖定的帳戶:
me.UnlockAccount();
遠遠高於普通的老容易System.DirectoryServices
的東西!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.