【发布时间】:2011-08-29 17:52:50
【问题描述】:
有谁知道 C# 和 VB.NET 中 DirectorySearcher 对象上 FindAll() 方法的实现是否有区别?据我了解,它们都被“编译”为 MSIL,并由 CLR 以相同的方式处理。针对我们的 ADAM/LDAP 系统,下面的 C# 代码会引发错误,而下面的 VB.NET 不会。
这是 C# 异常堆栈:
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.FindAll()
这是 C# 错误:
System.Runtime.InteropServices.COMException was unhandled
Message="The parameter is incorrect.\r\n"
Source="System.DirectoryServices"
ErrorCode=-2147024809
C#代码:
private void button1_Click(object sender, EventArgs e)
{
DirectoryEntry root = new DirectoryEntry("LDAP://directory.corp.com/OU=Person,OU=Lookups,O=Corp,C=US", null, null, AuthenticationTypes.Anonymous);
DirectorySearcher mySearcher = new DirectorySearcher(root);
mySearcher.Filter = "(uid=ssnlxxx)";
mySearcher.PropertiesToLoad.Add("cn");
mySearcher.PropertiesToLoad.Add("mail");
SearchResultCollection searchResultCollection = null;
searchResultCollection = mySearcher.FindAll(); //this is where the error occurs
try
{
foreach (SearchResult resEnt in searchResultCollection)
{
Console.Write(resEnt.Properties["cn"][0].ToString());
Console.Write(resEnt.Properties["mail"][0].ToString());
}
}
catch (DirectoryServicesCOMException ex)
{
MessageBox.Show("Failed to connect LDAP domain, Check username or password to get user details.");
}
}
这是有效的 VB.NET 代码:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim root As New DirectoryEntry("LDAP://directory.corp.com/OU=People,OU=Lookups,O=corp,C=US", vbNull, vbNull, authenticationType:=DirectoryServices.AuthenticationTypes.Anonymous)
Dim searcher As New DirectorySearcher(root)
searcher.Filter = "(uid=ssnlxxx)"
searcher.PropertiesToLoad.Add("cn")
searcher.PropertiesToLoad.Add("mail")
Dim results As SearchResultCollection
Try
results = searcher.FindAll()
Dim result As SearchResult
For Each result In results
Console.WriteLine(result.Properties("cn")(0))
Console.WriteLine(result.Properties("mail")(0))
Next result
Catch ex As Exception
MessageBox.Show("There was an error")
End Try
End Sub
【问题讨论】:
-
异常信息是什么?
-
我不确定这是否能解决您的问题(所以我在评论而不是“回答...”)但在您的 c# 代码中您可能会更改 @987654325 @到
@"LDAP://directory.corp.com/OU=Person,OU=Lookups,O=Corp,C=US"。 @ 符号告诉 C# 编译器忽略转义字符,这是 C# 和 VB.NET 之间可能直接影响此问题的少数区别之一。 -
VB.NET 代码中的
vbNull是什么?这与Nothing不同吗? -
我还注意到您在两种语言之间发现了不同的异常。尝试捕获相同的异常,看看是否得到相同的结果。
-
vbNull 是 VarType 返回的值,表示变体没有类型。它不等同于 c# NULL - 为此使用 vb 关键字 Nothing。
标签: c# .net vb.net exception-handling ldap