Lock/Unlock Account - Active Directory

Lock/Unlock Account Active Directory

 

Written by: Rickie Lee (http://www.cnblogs.com/rickie)

根据IsAccountLocked属性来判断Account是否Lock/Unlock,因为LDAP provider不支持IsAccountLocked属性,这里采用WinNT Provider:(注意Provider提供程序标识符区分大小写)

 

1. 获取IsAccountLocked属性,判断是否Lock/Unlock

string strUser =@"WinNT://MYDOMAIN/" + DOMAINUSERID;

DirectoryEntry de = new DirectoryEntry(strUser);

Object objusr = de.NativeObject;

Type t = objusr.GetType();

 

bool boolLocked = (bool)t.InvokeMember("IsAccountLocked", BindingFlags.GetProperty, null, objusr, null);

 

boolLocked = True 表示account is locked.

           = False 表示account is unlocked.

 

 

2. Lock/Unlock Account

// Unlock an Account

t.InvokeMember("IsAccountLocked", BindingFlags.SetProperty, null, objusr, new Object[] {"false"});

 

// Lock an Account

t.InvokeMember("IsAccountLocked", BindingFlags.SetProperty, null, objusr, new Object[] {"true"});


de.CommitChanges();

 

References:

1. Rickie, 更新Active Directory/Exchange Address Book的小工具, http://www.cnblogs.com/rickie/archive/2005/06/29/183043.html

2. Craig Aroa, ADHelper - An Active Directory Class,

http://www.c-sharpcorner.com/Code/2002/Sept/ADClass.asp

3. Rickie, 基于Active Directory的用户验证, http://www.cnblogs.com/rickie/archive/2005/06/30/183700.html

4. Rickie, Active Directory中获取用户信息, http://www.cnblogs.com/rickie/archive/2005/07/01/184289.html

5. Rickie, 更新Active Directory中用户信息, http://www.cnblogs.com/rickie/archive/2005/07/02/184927.html

 

原文地址:https://www.cnblogs.com/rickie/p/187524.html