我有以下代码
private static string ADValidateUser(string LDAP_URL, string username, string password)
{
System.DirectoryServices.DirectoryEntry myDE = new System.DirectoryServices.DirectoryEntry(LDAP_URL, username, password);
DirectorySearcher mySearcher = new DirectorySearcher(myDE);
mySearcher.Filter = ("sAMAccountName=" + username);
try
{
SearchResult myresult = mySearcher.FindOne();
string strName = myresult.GetDirectoryEntry().Properties["displayname"].Value.ToString();
string strCompany = myresult.GetDirectoryEntry().Properties["company"].Value.ToString();
return ("TRUE|" + (strName + ("|" + strCompany)).ToUpper());
}
catch (Exception ex)
{
return "FALSE|None";
}
}
我需要对此方法进行修改,以获取指定的用户信息,而无需输入密码,但只能输入用户名。
谢谢。
答案 0 :(得分:0)
这可行,但假设此进程作为有权访问LDAP_URL
中指定的目录的用户运行。
本质上,更改是调用DirectoryEntry(...)
构造函数,并省略使用当前用户的用户名和密码。
private static string ADValidateUser(string LDAP_URL, string username)
{
System.DirectoryServices.DirectoryEntry myDE = new System.DirectoryServices.DirectoryEntry(LDAP_URL);
DirectorySearcher mySearcher = new DirectorySearcher(myDE);
mySearcher.Filter = ("sAMAccountName=" + username);
try
{
SearchResult myresult = mySearcher.FindOne();
string strName = myresult.GetDirectoryEntry().Properties["displayname"].Value.ToString();
string strCompany = myresult.GetDirectoryEntry().Properties["company"].Value.ToString();
return ("TRUE|" + (strName + ("|" + strCompany)).ToUpper());
}
catch (Exception ex)
{
return "FALSE|None";
}
}