C#学习教程:如何检查LDAP上是否存在用户所以我需要一个像这样的方法publicboolUserExists(stringusername){...}我知道System.DirectoryServices命名空间但不知道从哪里开始。有任何想法吗?有超过80,000条记录,因此请记住这一点。谢谢。编辑:我做到了-我的代码是:privateboolUserExists(stringuserName,stringdomain){返回真;}catch(COMException){返回false;我不知道它是否正确,但到目前为止它似乎在工作。迈克尔的回答有两个相关部分:更新#2:我实际上使用过这个:publicstaticboolLoggedOnUserExists(){vardomain=newPrincipalContext(ContextType.Domain);UserPrincipalfoundUser=UserPrincipal.FindByIdentity(domain,IdentityType.SamAccountName,Environment.UserName);返回找到的用户!=null;在.NET3.5及更高版本中,您可以使用System.DirectoryServices.AccountManagement命名空间来完成此操作:publicboolUserExists(stringusername){//使用(PrincipalContextdomain=newPrincipalContext(ContextType.Domain)){创建域上下文//找到用户UserPrincipalfoundUser=UserPrincipal.FindByIdentity(domain,IdentityType.Name,username);返回找到的用户!=null;这将使用普通用户JohnDoe,或者您可以使用用户的电子邮件地址(john.doe@company.com)或他们的专有名称(CN=JohnDoe)-看看IdentityType枚举意味着引用:-)好文章开头:Howto:(Almost)EverythinginActiveDirectoryviaC#以上是C#学习教程:如何检查用户共享的所有内容是否存在于LDAP上。如果对大家有用,需要进一步了解C#学习教程,还望大家多多关注——本文收集自网络,不代表立场。如涉及侵权,请点击点击右侧联系管理员删除。如需转载请注明出处:
