如何检查用户是否存在于LDAP [英] How to check if a user exists on LDAP
问题描述
我需要验证只使用自己的用户名用户在该公司 - 不是他们的密码。
所以我需要一个像这样的方法
公共BOOL UserExists(字符串的用户名)
{...}
我知道的System.DirectoryServices
命名空间的,但不知道从哪里开始。
任何想法?
有80000条记录所以请尽量记住这一点。
感谢你。
编辑:
我已经做到了 - 我的code是:
私人布尔UserExists(用户名字符串,字符串域)
{
尝试
{
DirectoryEntry.Exists(WINNT://。[隐藏] .COM /+域名+用户名);
返回true;
}
赶上(收到COMException)
{
返回false;
}
}
我不知道这是否是正确的,但它似乎工作至今。
迈克尔的回答有两个相关的部分:
- HTTP://www.$c$cproject.com/ KB /系统/ everythingInAD.aspx#22
- HTTP://www.$c$cproject.com/ KB /系统/ everythingInAD.aspx#35
更新#2:
我实际使用这样的:
公共静态布尔LoggedOnUserExists()
{
VAR域=新PrincipalContext(ContextType.Domain);
UserPrincipal foundUser = UserPrincipal.FindByIdentity(域,IdentityType.SamAccountName,Environment.UserName);
返回foundUser!= NULL;
}
在.NET 3.5及以上,则可以使用 System.DirectoryServices.AccountManagement
命名空间来做到这一点很简单:
公共BOOL UserExists(字符串的用户名)
{
//创建您的域上下文
PrincipalContext域=新PrincipalContext(ContextType.Domain);
//找到用户
UserPrincipal foundUser = UserPrincipal.FindByIdentity(域,IdentityType.Name,用户名);
返回foundUser!= NULL;
}
这将与普通用户名李四
,或者您可以使用用户的电子邮件地址( john.doe@company .COM
),或者他的专有名称( CN =李四
) - 看看 IdentityType
枚举所提供的: - )
I need to verify users in the company using only their username - not their password.
So I need a method like this
public bool UserExists(string username)
{ ... }
I am aware of the System.DirectoryServices
namespace but don't know where to start.
Any ideas?
There are 80,000+ records so try to bear that in mind.
Thank you.
Edit:
I have done it - my code is:
private bool UserExists(string userName, string domain)
{
try
{
DirectoryEntry.Exists("WinNT://" + domain + ".[hidden].com/" + userName);
return true;
}
catch (COMException)
{
return false;
}
}
I don't know if it is correct, but it seems to work so far.
Michael's answer has two relevant parts:
- http://www.codeproject.com/KB/system/everythingInAD.aspx#22
- http://www.codeproject.com/KB/system/everythingInAD.aspx#35
Update #2:
I actually used this:
public static bool LoggedOnUserExists()
{
var domain = new PrincipalContext(ContextType.Domain);
UserPrincipal foundUser = UserPrincipal.FindByIdentity(domain, IdentityType.SamAccountName, Environment.UserName);
return foundUser != null;
}
In .NET 3.5 and up, you can use the System.DirectoryServices.AccountManagement
namespaces to do this quite simply:
public bool UserExists(string username)
{
// create your domain context
PrincipalContext domain = new PrincipalContext(ContextType.Domain);
// find the user
UserPrincipal foundUser = UserPrincipal.FindByIdentity(domain, IdentityType.Name, username);
return foundUser != null;
}
This will work with the regular user name John Doe
, or alternatively you can use the user's e-mail address (john.doe@company.com
), or his distinguished name (CN=John Doe
) - see what the IdentityType
enumeration has to offer :-)
这篇关于如何检查用户是否存在于LDAP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!