与VB.NET的LDAP连接 [英] LDAP Connection with VB.NET

查看:83
本文介绍了与VB.NET的LDAP连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在用vb.net启动ASP.net

为了测试脚本,除了Visual Studio Web开发人员2k8 Express,我还安装了Visual Studio Express 2k8

我尝试使用以下代码连接到我的ldap:

Hi everybody,

i''m starting ASP.net with vb.net

in order to test scripts i have installed visual studio express 2k8 in addition to visual studio web developer 2k8 express

i try to connect to my ldap with this code:

<pre lang="vb">Imports System.DirectoryServices<br />
Module Module1<br />
    Sub Main()<br />
GetDirectoryEntry()<br />
    End Sub<br />
    Public Function GetDirectoryEntry() As DirectoryEntry<br />
        Dim de As DirectoryEntry = New DirectoryEntry()<br />
        de.Path = "LDAP://"<br />
        de.Username = "yourdomain\sampleuser"<br />
        de.Password = "samplepassword"<br />
        Return de<br />
    End Function<br />
<br />
End Module</pre><br />



从此处

您能告诉我如何查看是否与这样的文本消息相连:"AD已连接" ,因为我一直有此按任意键继续...

谢谢

国王的问候

Samuel



from here

Could you tell me how i can see if there is connected with a text message like this: "AD connected" because i have always this Press any key to continue ...

Thanks

Kings Regards

Samuel

推荐答案

如果我正确理解了您的问题,我会这样做:
If I understand your question correctly, I would do it like this:
Public Function GetDirectoryEntry(ByVal Path As String) As DirectoryEntry
    Dim Result As DirectoryEntry = Nothing

    Try
        Result = New DirectoryEntry(Path)
        Dim Test As System.Guid = Result.Guid
    Catch ex As Exception
        Result.Close
        Result = Nothing
    End Try

    Return Result
End Function

Public Sub Main
    Dim DirEntry As DirectoryEntry = GetDirectoryEntry(Path)
    If DirEntry Is Nothing Then
        ''The path is invalid
    Else
        ''The path is valid
        ''Use it
        DirEntry.Close
    End If
End Sub


如果 Path 有效,则该函数将返回一个打开的有效DirectoryEntry对象.否则,它将返回Nothing.

使用Try/Catch块来确定有效值是错误的形式,但是Active Directory似乎没有给我们任何其他选择.无论路径如何,即使未找到路径,您都将得到DirectoryEntry.知道对象是否有效的唯一方法是查看其值之一并失败.至少这是我的经验.

更新::doh:该函数返回一个打开的DirectoryEntry;它需要关闭,否则会发生内存泄漏.我在Main上添加了Dim语句,以保存该函数返回的对象.


If Path is valid, then the function will return an open, valid DirectoryEntry object. Otherwise, it will return Nothing.

Using Try/Catch blocks to determine valid values is bad form, but Active Directory doesn''t seem to give us any other choice. Regardless of Path, you will get your DirectoryEntry even if the path was not found. The only way to know if the object is valid is to look at one of its values and fail. At least, that is my experience.

Update: :doh: The function is returning an open DirectoryEntry; it will need to be closed, otherwise you have a memory leak. I added the Dim statement to Main to hold the object returned by the function; do with it as you will.


感谢Gregory.Gadow,您很快:)

我会尝试
thanks Gregory.Gadow, you''re fast :)

I will try


这篇关于与VB.NET的LDAP连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆