指定的域不存在或无法联系
The specified domain either does not exist or could not be contacted
我正尝试通过 docker 中设置的 ldap 目录访问 access/authenticate 用户,但不断出现上述错误
https://github.com/wshihadeh/ldap_server
我的代码如下
LdapAuthentication ldap = new LdapAuthentication("LDAP://cn=developer,dc=shihadeh,dc=intern");
ldap.IsAuthenticated("LDAP://shihadeh.intern", "uid=developer", "developer_pass");
public class LdapAuthentication
{
private String _path;
private String _filterAttribute;
public LdapAuthentication(String path)
{
_path = path;
}
public bool IsAuthenticated(String domain, String username, String pwd)
{
String domainAndUsername = domain + @"\" + username;
DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd, AuthenticationTypes.None);
try
{ //Bind to the native AdsObject to force authentication.
Object obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName =" + username + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if (null == result)
{
return false;
}
//Update the new path to the user in the directory.
_path = result.Path;
_filterAttribute = (String)result.Properties["cn"][0];
}
catch (Exception ex)
{
throw new Exception("Error authenticating user. " + ex.Message);
}
return true;
}
public String GetGroups()
{
DirectorySearcher search = new DirectorySearcher(_path);
search.Filter = "(cn =" + _filterAttribute + ")";
search.PropertiesToLoad.Add("memberOf");
StringBuilder groupNames = new StringBuilder();
try
{
SearchResult result = search.FindOne();
int propertyCount = result.Properties["memberOf"].Count;
String dn;
int equalsIndex, commaIndex;
for (int propertyCounter = 0; propertyCounter < propertyCount; propertyCounter++)
{
dn = (String)result.Properties["memberOf"][propertyCounter];
equalsIndex = dn.IndexOf("=", 1);
commaIndex = dn.IndexOf(",", 1);
if (-1 == equalsIndex)
{
return null;
}
groupNames.Append(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1));
groupNames.Append("|");
}
}
catch (Exception ex)
{
throw new Exception("Error obtaining group names. " + ex.Message);
}
return groupNames.ToString();
}
}
知道我做错了什么吗。
也尝试使用以下代码但出现错误 LDAP 服务器是
不可用。
try
{
string username = "developer";
string password = "developer_pass";
string filter = "(&(&(objectclass=user)(objectcategory=person))" +
"sAMAccountName=username)";
NetworkCredential credentials = new NetworkCredential(username, password);
LdapDirectoryIdentifier directoryIdentifier =
new LdapDirectoryIdentifier("LDAP://127.0.0.1/cn=developer,dc=shihadeh,dc=intern", 389, false, false);
using (LdapConnection connection =
new LdapConnection(directoryIdentifier, credentials, AuthType.Basic))
{
connection.Timeout = new TimeSpan(0, 0, 90);
connection.SessionOptions.ProtocolVersion = 3;
SearchRequest search =
new SearchRequest(username, filter, System.DirectoryServices.Protocols.SearchScope.Base, "mail");
SearchResponse response = connection.SendRequest(search) as SearchResponse;
foreach (SearchResultEntry entry in response.Entries)
{
Console.WriteLine(entry.Attributes["mail"][0]);
}
}
}
catch (Exception ex)
{
}
如果有在线 运行 可用的 ldap 免费服务器,那么我也可以尝试使用它进行测试
以下代码有效
try
{
// don't add LDAP://, the protocol is already known ...
LdapConnection ldapConnection = new LdapConnection("127.0.0.1:389");
// notice we don't use the domain here
var networkCredential = new NetworkCredential(
"cn=developer,dc=shihadeh,dc=intern",
"developer_pass");
// Apache Directory Server uses LDAPv3
ldapConnection.SessionOptions.ProtocolVersion = 3;
// 10389 is the plain port, no ssl needed
//ldapConnection.SessionOptions.SecureSocketLayer = true;
// ldapConnection.SessionOptions.VerifyServerCertificate += delegate { return true; };
// let's not negotiate, only Basic is supported
ldapConnection.AuthType = AuthType.Basic;
ldapConnection.Bind(networkCredential);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
我正尝试通过 docker 中设置的 ldap 目录访问 access/authenticate 用户,但不断出现上述错误 https://github.com/wshihadeh/ldap_server 我的代码如下
LdapAuthentication ldap = new LdapAuthentication("LDAP://cn=developer,dc=shihadeh,dc=intern");
ldap.IsAuthenticated("LDAP://shihadeh.intern", "uid=developer", "developer_pass");
public class LdapAuthentication
{
private String _path;
private String _filterAttribute;
public LdapAuthentication(String path)
{
_path = path;
}
public bool IsAuthenticated(String domain, String username, String pwd)
{
String domainAndUsername = domain + @"\" + username;
DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd, AuthenticationTypes.None);
try
{ //Bind to the native AdsObject to force authentication.
Object obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName =" + username + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if (null == result)
{
return false;
}
//Update the new path to the user in the directory.
_path = result.Path;
_filterAttribute = (String)result.Properties["cn"][0];
}
catch (Exception ex)
{
throw new Exception("Error authenticating user. " + ex.Message);
}
return true;
}
public String GetGroups()
{
DirectorySearcher search = new DirectorySearcher(_path);
search.Filter = "(cn =" + _filterAttribute + ")";
search.PropertiesToLoad.Add("memberOf");
StringBuilder groupNames = new StringBuilder();
try
{
SearchResult result = search.FindOne();
int propertyCount = result.Properties["memberOf"].Count;
String dn;
int equalsIndex, commaIndex;
for (int propertyCounter = 0; propertyCounter < propertyCount; propertyCounter++)
{
dn = (String)result.Properties["memberOf"][propertyCounter];
equalsIndex = dn.IndexOf("=", 1);
commaIndex = dn.IndexOf(",", 1);
if (-1 == equalsIndex)
{
return null;
}
groupNames.Append(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1));
groupNames.Append("|");
}
}
catch (Exception ex)
{
throw new Exception("Error obtaining group names. " + ex.Message);
}
return groupNames.ToString();
}
}
知道我做错了什么吗。
也尝试使用以下代码但出现错误 LDAP 服务器是 不可用。
try
{
string username = "developer";
string password = "developer_pass";
string filter = "(&(&(objectclass=user)(objectcategory=person))" +
"sAMAccountName=username)";
NetworkCredential credentials = new NetworkCredential(username, password);
LdapDirectoryIdentifier directoryIdentifier =
new LdapDirectoryIdentifier("LDAP://127.0.0.1/cn=developer,dc=shihadeh,dc=intern", 389, false, false);
using (LdapConnection connection =
new LdapConnection(directoryIdentifier, credentials, AuthType.Basic))
{
connection.Timeout = new TimeSpan(0, 0, 90);
connection.SessionOptions.ProtocolVersion = 3;
SearchRequest search =
new SearchRequest(username, filter, System.DirectoryServices.Protocols.SearchScope.Base, "mail");
SearchResponse response = connection.SendRequest(search) as SearchResponse;
foreach (SearchResultEntry entry in response.Entries)
{
Console.WriteLine(entry.Attributes["mail"][0]);
}
}
}
catch (Exception ex)
{
}
如果有在线 运行 可用的 ldap 免费服务器,那么我也可以尝试使用它进行测试
以下代码有效
try
{
// don't add LDAP://, the protocol is already known ...
LdapConnection ldapConnection = new LdapConnection("127.0.0.1:389");
// notice we don't use the domain here
var networkCredential = new NetworkCredential(
"cn=developer,dc=shihadeh,dc=intern",
"developer_pass");
// Apache Directory Server uses LDAPv3
ldapConnection.SessionOptions.ProtocolVersion = 3;
// 10389 is the plain port, no ssl needed
//ldapConnection.SessionOptions.SecureSocketLayer = true;
// ldapConnection.SessionOptions.VerifyServerCertificate += delegate { return true; };
// let's not negotiate, only Basic is supported
ldapConnection.AuthType = AuthType.Basic;
ldapConnection.Bind(networkCredential);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}