[英]Using LDAP in WPF to find specific user in an Active Directory
In WPF and therefore in the C# programming language, I am trying to use LDAP to find a specific user within an Active Directory. 在WPF中,因此在C#编程语言中,我试图使用LDAP在Active Directory中查找特定用户。 I was able to check if a specific user exists in an Active Directory but I was not able to retrieve that user from the directory in order to gain access to their properties.
我能够检查Active Directory中是否存在特定用户,但是我无法从目录中检索该用户以获取对其属性的访问权限。
I am using the System.DirectoryServices namespace. 我正在使用System.DirectoryServices命名空间。
Is there a way to do what I seek to achieve, is there a way to retrieve a specific user from the AD using LDAP in order to check their properties? 有没有办法实现我要达到的目标,有没有办法使用LDAP从AD中检索特定用户以检查其属性?
EDIT: code I used to check if user is in AD. 编辑:我用来检查用户是否在AD中的代码。 Returns true if user is in AD, false if user is not found.
如果用户在AD中,则返回true;如果找不到用户,则返回false。 I wonder thought if there is a limit to the number of users it will search.
我想知道是否会限制搜索的用户数量。
bool ContainsUser(string domain, string userName)
{
string ldapBase = string.Format("LDAP://{0}", domain);
using (var entry = new DirectoryEntry(ldapBase))
{
using (var searcher = new DirectorySearcher(entry))
{
searcher.Filter = string.Format("(sAMAccountName={0})", userName);
return searcher.FindOne() != null;
}
}
}
You should investigate using UserPrincipal.FindByIdentity
您应该使用
UserPrincipal.FindByIdentity
进行调查
for example: 例如:
public static string GetEmailAddressFromActiveDirectoryUserName(string adUserName)
{
string email = string.Empty;
if (!string.IsNullOrEmpty(adUserName))
{
using (var pctx = new PrincipalContext(ContextType.Domain))
{
using (UserPrincipal up = UserPrincipal.FindByIdentity(pctx, adUserName))
{
return !string.IsNullOrEmpty(up?.EmailAddress) ? up.EmailAddress : string.Empty;
}
}
}
return email;
}
See: 看到:
https://docs.microsoft.com/en-us/dotnet/api/system.directoryservices.accountmanagement.userprincipal.findbyidentity?view=netframework-4.8 https://docs.microsoft.com/en-us/dotnet/api/system.directoryservices.accountmanagement.userprincipal.findbyidentity?view=netframework-4.8
The code you use to check that the user exists in AD or not already loads user properties: searcher.FindOne()?.Properties
. 用于检查用户是否存在于AD中或尚未加载用户属性的代码:
searcher.FindOne()?.Properties
。
public class User
{
public string UserPrincipalName { get; set; }
public string Name { get; set; }
}
User GetAdUser(string domain, string userName)
{
string ldapBase = string.Format("LDAP://{0}", domain);
using (var entry = new DirectoryEntry(ldapBase))
{
using (var searcher = new DirectorySearcher(entry))
{
searcher.Filter = string.Format("(sAMAccountName={0})", userName);
var result = searcher.FindOne();
User user = null;
if (result != null)
{
// result.Properties - list of loaded user properties
// result.Properties.PropertyNames - list of user property names
user = new User
{
UserPrincipalName = result.Properties["userprincipalname"].Cast<string>().FirstOrDefault();
Name = result.Properties["name"].Cast<string>().FirstOrDefault();
}
}
return user;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.