Nob*_*ody 5 c# ldap .net-4.0 active-directory
我需要使用他们的用户名验证公司中的用户 - 而不是他们的密码.
所以我需要一个像这样的方法
public bool UserExists(string username)
{ ... }
Run Code Online (Sandbox Code Playgroud)
我知道System.DirectoryServices命名空间但不知道从哪里开始.
有任何想法吗?
有80,000多条记录,所以请记住这一点.
谢谢.
编辑:
我做到了 - 我的代码是:
private bool UserExists(string userName, string domain)
{
try
{
DirectoryEntry.Exists("WinNT://" + domain + ".[hidden].com/" + userName);
return true;
}
catch (COMException)
{
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道它是否正确,但它似乎到目前为止工作.
迈克尔的答案有两个相关部分:
更新#2:
我实际上用过这个:
public static bool LoggedOnUserExists()
{
var domain = new PrincipalContext(ContextType.Domain);
UserPrincipal foundUser = UserPrincipal.FindByIdentity(domain, IdentityType.SamAccountName, Environment.UserName);
return foundUser != null;
}
Run Code Online (Sandbox Code Playgroud)
在.NET 3.5及更高版本中,您可以使用System.DirectoryServices.AccountManagement命名空间来完成此操作:
public bool UserExists(string username)
{
// create your domain context
using (PrincipalContext domain = new PrincipalContext(ContextType.Domain))
{
// find the user
UserPrincipal foundUser = UserPrincipal.FindByIdentity(domain, IdentityType.Name, username);
return foundUser != null;
}
}
Run Code Online (Sandbox Code Playgroud)
这将使用常规用户名John Doe,或者您可以使用用户的电子邮件地址(john.doe@company.com)或他的专有名称(CN=John Doe) - 查看IdentityType枚举提供的内容:-)
| 归档时间: |
|
| 查看次数: |
13668 次 |
| 最近记录: |