要在本地ADFS中访问LDAP,您可以使用以下代码示例的解决方法:
using System.DirectoryServices;
// 设置LDAP连接参数
string ldapPath = "LDAP://example.com"; // LDAP服务器的地址
string username = "username"; // LDAP用户名
string password = "password"; // LDAP密码
// 创建LDAP连接
DirectoryEntry ldapConnection = new DirectoryEntry(ldapPath, username, password);
// 搜索AD中的对象
DirectorySearcher ldapSearcher = new DirectorySearcher(ldapConnection);
ldapSearcher.Filter = "(objectClass=user)"; // 这里可以根据需要设置搜索过滤器
ldapSearcher.PropertiesToLoad.Add("cn"); // 这里可以根据需要设置要加载的属性
// 执行搜索操作
SearchResultCollection ldapResults = ldapSearcher.FindAll();
// 处理搜索结果
foreach (SearchResult ldapResult in ldapResults)
{
if (ldapResult.Properties["cn"].Count > 0)
{
string commonName = ldapResult.Properties["cn"][0].ToString();
Console.WriteLine("Common Name: " + commonName);
}
}
// 关闭LDAP连接
ldapConnection.Close();
请注意,上述代码示例假设您具有正确的LDAP服务器地址、用户名和密码,并且您可以根据需要自定义搜索过滤器和要加载的属性。另外,确保您的应用程序具有访问LDAP服务器的权限。