我在连接到ldap时遇到问题。它一直给我一个COMExceptionError(参数不正确)
这是我到目前为止的代码:
static void Main(string[] args)
{
DirectoryEntry ldapConnection = new DirectoryEntry("10.9.130.113:667");
ldapConnection.Path = "LDAP://ou=Users,ou=CorporateStore,ou=Absa,c=za";
ldapConnection.AuthenticationType = AuthenticationTypes.Anonymous;
DirectorySearcher ds = new DirectorySearcher(ldapConnection);
SearchResult result = ds.FindOne();
Console.ReadLine();
if (result != null)
{
ResultPropertyCollection fields = result.Properties;
foreach (String ldapField in fields.PropertyNames)
{
foreach (Object myCollection in fields[ldapField])
Console.WriteLine(String.Format("{0,-20} : {1}",
ldapField, myCollection.ToString()));
Console.ReadLine();
}
这是错误发生在:
SearchResult result = ds.findOne();
继承异常错误和堆栈跟踪:
System.Runtime.InteropServices.COMException was unhandled
Message=The parameter is incorrect.
Source=System.DirectoryServices
ErrorCode=-2147024809
StackTrace:
at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
at System.DirectoryServices.DirectoryEntry.Bind()
at System.DirectoryServices.DirectoryEntry.get_AdsObject()
at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne)
at System.DirectoryServices.DirectorySearcher.FindOne()
at LDAPConnector.Program.Main(String[] args) in c:\documents and settings\expn261\my documents\visual studio 2010\Projects\LDAPConnector\LDAPConnector\Program.cs:line 23
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
有什么想法吗?
答案 0 :(得分:1)
您必须指定要加载的一些属性才能使findone()方法起作用。 在此示例中,尝试查找用户的属性(用户名是strig变量)。
DirectoryContext context = new DirectoryContext(DirectoryContextType.Domain, domain); //domain is a string with the FQDN (ex: int.domain.local) or alias (es: mydomainname)
DomainControllerCollection dcc = DomainController.FindAll(context);
DirectorySearcher ds;
ds = dcc[0].GetDirectorySearcher();
ds.Filter = String.Format("(&(sAMAccountName={0})(objectClass=user))", username);
ds.PropertiesToLoad.Add("lastLogon");
ds.PropertiesToLoad.Add("displayName");
ds.PropertiesToLoad.Add("memberOf");
ds.PropertiesToLoad.Add("userAccountControl");
ds.PropertiesToLoad.Add("ADSPath");
ds.PropertiesToLoad.Add("PrimaryGroupID");
ds.PropertiesToLoad.Add("pwdLastSet");
ds.PropertiesToLoad.Add("maxPwdAge");
ds.PropertiesToLoad.Add("mail");
ds.PropertiesToLoad.Add("distinguishedName");
ds.PropertiesToLoad.Add("mdbstoragequota");
ds.PropertiesToLoad.Add("SamAccountName");
ds.SizeLimit = 15;
SearchResult sr = ds.FindOne();
答案 1 :(得分:1)
尝试以下方法:
ldapConnection.Path
答案 2 :(得分:0)
似乎您在构造函数中为DirectoryEntry定义了不同的路径,然后通过设置Path属性来覆盖它。如果您的服务器与RDN中的域不同,则应在路径中定义它。您是否可以尝试以这种方式执行此操作并查看是否出现其他错误?
DirectoryEntry ldapConnection = new DirectoryEntry("LDAP://10.9.130.113:667/ou=Users,ou=CorporateStore,ou=Absa,dc=za");
并跳过通过财产设置路径的部分。
编辑:通知您似乎错过了dc = za上的“d”。