我正在研究需要对LDAP AD进行身份验证的.net核心项目,我正在使用Novell库来帮助我连接到LDAP AD Server,但是当我运行代码时,大约需要2分钟才能得到响应。谁能告诉我怎么了?供您参考,我在ubuntu服务器上托管.net核心项目,而LDAP AD服务器在Windows Azure服务器中。
这是代码,谢谢。
public ADResponse LdapSearch(string server, string filter, string username, string password)
{
try
{
using (var connection = new LdapConnection { SecureSocketLayer = false })
{
string DN = configuration.GetValue<string>("LdapServer:TeacherDN");
if (server == configuration.GetValue<string>("LdapServer:LdapStudent"))
{
DN = configuration.GetValue<string>("LdapServer:sphStudentDN");
}
int serverPort = configuration.GetValue<int>("LdapServer:sphLdapPort");
string[] attributes = { "name", "mail", "extensionAttribute2", "memberOf" };
connection.Connect(server, serverPort);
connection.Bind(username, password);
if (connection.Bound)
{
ADResponse data = new ADResponse();
LdapSearchQueue queue = connection.Search(DN,
LdapConnection.SCOPE_SUB,
filter,
attributes,
false,
(LdapSearchQueue)null,
(LdapSearchConstraints)null);
LdapMessage message;
while ((message = queue.getResponse()) != null)
{
if (message is LdapSearchResult)
{
LdapEntry entry = ((LdapSearchResult)message).Entry;
LdapAttributeSet attributeSet = entry.getAttributeSet();
System.Collections.IEnumerator ienum = attributeSet.GetEnumerator();
while (ienum.MoveNext())
{
LdapAttribute attribute = (LdapAttribute)ienum.Current;
string attributeName = attribute.Name;
string attributeVal = attribute.StringValue;
if (attributeName == "name")
{
data.name = attributeVal;
}
else if (attributeName == "mail")
{
data.mail = attributeVal;
}
else if (attributeName.ToLower() == "postofficebox")
{
data.pobox = attributeVal;
}
else if (attributeName.ToLower() == "extensionattribute2")
{
data.extensionattribute = attributeVal;
}
else if (attributeName.ToLower() == "memberof")
{
data.memberof = attributeVal;
}
}
}
}
connection.Disconnect();
return data;
}
}
}
catch (LdapException ex)
{
// Log exception
}
return null;
}
更新:
在Windows服务器中查询的速度要快得多,因此我已经尝试将其托管在Windows服务器上,而查询ldap的过程仅需5秒钟。有人知道为什么会这样吗?
更新:
我试图从ubuntu服务器访问另一个AD(From this public test AD),并且运行平稳,并且只花了2秒钟进行查询。