Visual studio 2010 带有LDAP连接错误的AD
我正在尝试使用.net应用程序,但该应用程序在我的本地网络中找不到服务器 Im使用LdapExploreTool 2进行以下设置: 连接成功,结果如下:Visual studio 2010 带有LDAP连接错误的AD,visual-studio-2010,active-directory,ldap,windows-server-2003,Visual Studio 2010,Active Directory,Ldap,Windows Server 2003,我正在尝试使用.net应用程序,但该应用程序在我的本地网络中找不到服务器 Im使用LdapExploreTool 2进行以下设置: 连接成功,结果如下: 输入值: 检查代码时,我发现“绑定到本机AdsObject以强制身份验证”时出现异常: “指定的域不存在或无法联系。” public bool已验证(字符串域、字符串ldapPath、字符串用户名、字符串pwd、字符串userToValidate) { 字符串domainAndUsername=域+@“\”+用户名; if(string.
输入值:
检查代码时,我发现“绑定到本机AdsObject以强制身份验证”时出现异常: “指定的域不存在或无法联系。”
public bool已验证(字符串域、字符串ldapPath、字符串用户名、字符串pwd、字符串userToValidate)
{
字符串domainAndUsername=域+@“\”+用户名;
if(string.IsNullOrEmpty(ldapPath))
SetLdapPath(域);
其他的
_路径=ldapPath;
App.Services.Log.LogUtils.WriteLog(Log.LogLevel.INFO,“IsAuthenticated_DirectoryEntry:“+_path+”、“+domainAndUsername+”、“+pwd”);
DirectoryEntry=新的DirectoryEntry(_路径,域和用户名,pwd);
//检查域是否有效
int domainId=AppDomains.GetDomainIdByName(域);
if(domainId==int.MinValue)
{
返回false;
}
AppDomains d=AppDomains.GetRecord(domainId);
List lQueries=新列表(AppDomainQueries.GetArray());
lQueries=lQueries.FindAll(委托(AppDomainQueries dq){return dq.DomainId==DomainId&&dq.Status=='A';});
string queryString=string.Empty;
尝试
{
//绑定到本机对象以强制身份验证。
Object obj=entry.NativeObject;
DirectorySearcher search=新的DirectorySearcher(条目);
string ldapandquerye=string.Empty;
//基本帐户搜索
queryString=“(SAMAccountName=“+userToValidate+”);
如果(用户名!=userToValidate)
{
if(lquerys.Count==1)
ldapAndQuerie=lQueries.FirstOrDefault().QueryString;
如果((ldapandquerye!=string.Empty)&&&(ldapandquerye!=“*”)&&(ldapandquerye!=“(objectClass=user)”)
queryString=“(&(SAMAccountName=“+userToValidate+”)”+ldapAndQuerie+”);
}
search.Filter=queryString;
App.Services.Log.LogUtils.WriteLog(Log.LogLevel.INFO,“LDAP=“+queryString”);
search.PropertiesToLoad.Add(“cn”);
SearchResult=search.FindOne();
if(null==结果)
{
返回false;
}
//将新路径更新到目录中的用户
_路径=结果。路径;
_filterAttribute=(字符串)result.Properties[“cn”][0];
}
捕获(例外情况除外)
{
App.Services.Log.LogUtils.WriteLog(Log.LogLevel.ERROR,“App.Services.Core.ldapaauthentication.IsAuthenticated()异常-(LDAP=“+queryString+”)”+ex.Message,ex);
返回false;
}
返回true;
}
如何建立连接?问题是LDAP连接字符串, 它似乎错过了网络中的实际位置(IP+端口)
LDAP://192.168.1.250:389/DC=exago,DC=local
public bool IsAuthenticated(string domain, string ldapPath, string username, string pwd, string userToValidate)
{
string domainAndUsername = domain + @"\" + username;
if (string.IsNullOrEmpty(ldapPath))
SetLdapPath(domain);
else
_path = ldapPath;
App.Services.Log.LogUtils.WriteLog(Log.LogLevel.INFO, "IsAuthenticated_DirectoryEntry:" + _path + "," + domainAndUsername + "," + pwd);
DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);
//check if domain is valid
int domainId = AppDomains.GetDomainIdByName(domain);
if (domainId == int.MinValue)
{
return false;
}
AppDomains d = AppDomains.GetRecord(domainId);
List<AppDomainQueries> lQueries = new List<AppDomainQueries>(AppDomainQueries.GetArray());
lQueries = lQueries.FindAll(delegate(AppDomainQueries dq) { return dq.DomainId == domainId && dq.Status == 'A'; });
string queryString = string.Empty;
try
{
// Bind to the native AdsObject to force authentication.
Object obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry);
string ldapAndQuerie = string.Empty;
//base account search
queryString = "(SAMAccountName=" + userToValidate + ")";
if (username != userToValidate)
{
if (lQueries.Count == 1)
ldapAndQuerie = lQueries.FirstOrDefault().QueryString;
if ((ldapAndQuerie != string.Empty) && (ldapAndQuerie != "*") && (ldapAndQuerie != "(objectClass = user)"))
queryString = "(&(SAMAccountName=" + userToValidate + ")" + ldapAndQuerie + ")";
}
search.Filter = queryString;
App.Services.Log.LogUtils.WriteLog(Log.LogLevel.INFO, "LDAP=" + queryString);
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)
{
App.Services.Log.LogUtils.WriteLog(Log.LogLevel.ERROR, "App.Services.Core.LdapAuthentication.IsAuthenticated() Exception - (LDAP=" + queryString + ")" + ex.Message, ex);
return false;
}
return true;
}
LDAP://192.168.1.250:389/DC=exago,DC=local