2016-05-30 75 views
0

也許這是一個愚蠢的問題,但我真的是新的這個領域.. 我正在使用AD在vb.net的身份驗證。在做了一些搜索之後,我發現了很多與此相關的代碼。例如:測試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 

如何知道代碼是否工作?我必須製作登錄表單嗎?

+0

創建一個函數,該函數包含返回true或false(基於dirSearcher.FindOne())的最小邏輯(例如DirectoryService但不包含UI元素)和參數(Domain Name,User Name)。創建調用第一個函數的另一個函數(或單元測試)。第二個功能可以是一個控制檯應用程序,例如 –

回答

0

只需創建一個帶有文本框/標籤= lblStatus的窗體並運行該功能。