c# - Active Directory LDAP - 锁定用户帐户

标签 c# asp.net .net active-directory

锁定 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 article Managing Directory Security Principals in the .NET Framework 3.5 .

对于您的情况,您必须以某种方式获取 UserPrincipal,例如

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN");
UserPrincipal me = UserPrincipal.Current;

然后您可以访问大量非常易于使用的属性和方法 - 例如:

bool isLockedOut = me.IsAccountLockedOut();

您可以使用以下方式解锁锁定的帐户:

me.UnlockAccount();

MUCH 比普通的旧 System.DirectoryServices 东西更容易!

关于c# - Active Directory LDAP - 锁定用户帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2768382/

相关文章:

c# - 管理 C# 中返回的字符串指针

C# IIS 7.5 类未注册异常

c# - <%# 的默认值(绑定(bind)或评估)

asp.net - 如何在浏览器中查看wcf服务

javascript - jQuery 事件不会在动态生成的内容上触发

c# - 编写一个简单的工作流系统

c# - C# 中 try/finally 的开销?

.net - System.String是一个类类型,但是为什么我们不用New关键字实例化就可以使用它呢?

c# - ASP.NET MVC 6 中的 TryValidateModel

c# - 我可以在 C# 中为匿名类指定一个有意义的名称吗?