测试vb.net代码是否有效

时间:2016-05-30 06:12:19

标签: vb.net authentication active-directory

也许这是一个愚蠢的问题,但我在这个领域真的很新...... 我正在使用AD在vb.net中进行身份验证。在做了一些搜索后,我发现了很多与此相关的代码。例如:

Private m_ServerName As String 
Private m_LoginName As String
Private m_Authenicate As String 
Public Sub New() 
' This call is required by the Windows Form Designer. 
InitializeComponent()  

' Add any initialization after the InitializeComponent() call. 
m_ServerName = DOMAIN_NAME ' Your Domain Name 
m_LoginName = Environment.UserName.ToString 
m_Authenicate = My.User.Name  
End Sub  

Public Function IsLogonValid() As Boolean 
Dim m_LoginName As String 
Dim dirEntry As System.DirectoryServices.DirectoryEntry 
Dim dirSearcher As System.DirectoryServices.DirectorySearcher
lblStatus.Text = "Validating User Account"  

Try  
m_LoginName = Environment.UserName.ToString 'The logged in user ID 
dirEntry = New System.DirectoryServices.DirectoryEntry("LDAP://" & DOMAIN_NAME)  
dirSearcher = New System.DirectoryServices.DirectorySearcher(dirEntry)  
dirSearcher.Filter = "(samAccountName=" & m_LoginName & ")" 
       'Use the .FindOne() Method to stop as soon as a match is found 

Dim sr As SearchResult = dirSearcher.FindOne()  

If sr Is Nothing Then 'return false if user isn't found 
lblStatus.Text = "User authentication failed" 
Return False 
End If 
Dim de As System.DirectoryServices.DirectoryEntry = sr.GetDirectoryEntry() 
sUserName = de.Properties("GivenName").Value.ToString() 

lblStatus.Text = "User authentication success" 

Return True  'Valid user 

Catch ex As Exception ' return false if exception occurs 
lblStatus.Text = "User authentication failed" 

Return False 

End Try 

End Function 

我如何知道代码是否有效?我是否必须登录表单?

1 个答案:

答案 0 :(得分:0)

只需创建一个带有textbox / label = lblStatus的表单并运行该函数。