ตอนแรกกะจะ เอาไปอยู่ใน IF ที่มัน false อะครับผม แต่ถ้าทำ มันจะ query ช้าไหมครับ
ถ้าเป็นแบบข้างล่าง ผมจะเปลี่ยนที่ยังไงหรอครับ
Code (VB.NET)
Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs)
public Notinheritable Class PathActive
Dim initLDAPPath As String = "DC=opsd,DC=net"
Dim initLDAPServer As String = "10.0.4.32"
Dim initShortDomainName As String = "OPSD"
end class
public Notinheritable Class PathActive1
Dim initLDAPPath1 As String = "DC=test,DC=net"
Dim initLDAPServer1 As String = "1.0.0.33"
Dim initShortDomainName1 As String = "test"
end class
strCommu = "LDAP://" & initLDAPServer & "/" & initLDAPPath
DomainAndUsername = initShortDomainName & "\" & txtUser.Text
Dim entry As New DirectoryEntry(strCommu, DomainAndUsername, txtPwd.Text)
Dim obj As Object
Try
obj = entry.NativeObject
Dim search As New DirectorySearcher(entry)
Dim result As SearchResult
search.Filter = "(SAMAccountName=" + txtUser.Text + ")"
search.PropertiesToLoad.Add("cn")
result = search.FindOne()
If result Is Nothing Then
flgLogin = False
strErrMsg = "Please check user/password"
Else
flgLogin = True
End If
Catch ex As Exception
flgLogin = False
strErrMsg = "Please check user/password"
End Try
IF flgLogin = True
Me.lbDisplay.Text = "Welcome " & txtUser.Text
Else
Me.lbDisplay.Text = strErrMsg
End IF
End Sub
Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim initLDAPPath As String = "DC=opsd,DC=net"
Dim initLDAPServer As String = "10.0.4.32"
Dim initShortDomainName As String = "OPSD"
Dim initLDAPPath1 As String = "DC=test,DC=net"
Dim initLDAPServer1 As String = "1.0.0.33"
Dim initShortDomainName1 As String = "test"
strCommu = "LDAP://" & initLDAPServer & "/" & initLDAPPath
DomainAndUsername = initShortDomainName & "\" & txtUser.Text
Dim entry As New DirectoryEntry(strCommu, DomainAndUsername, txtPwd.Text)
Dim obj As Object
Try
obj = entry.NativeObject
Dim search As New DirectorySearcher(entry)
Dim result As SearchResult
search.Filter = "(SAMAccountName=" + txtUser.Text + ")"
search.PropertiesToLoad.Add("cn")
result = search.FindOne()
If result Is Nothing Then
flgLogin = False
strErrMsg = "Please check user/password" ' เอามาไว้ข้างในแบบนี้เลยหรอครับ
Dim initLDAPPath1 As String = "DC=test,DC=net"
Dim initLDAPServer1 As String = "1.0.0.33"
Dim initShortDomainName1 As String = "test"
strCommu = "LDAP://" & initLDAPServer & "/" & initLDAPPath
DomainAndUsername = initShortDomainName & "\" & txtUser.Text
Dim entry As New DirectoryEntry(strCommu, DomainAndUsername, txtPwd.Text)
Dim obj As Object
Try
obj = entry.NativeObject
Dim search As New DirectorySearcher(entry)
Dim result As SearchResult
search.Filter = "(SAMAccountName=" + txtUser.Text + ")"
search.PropertiesToLoad.Add("cn")
result = search.FindOne()
If result Is Nothing Then
flgLogin = False
strErrMsg = "Please check user/password"
Else
flgLogin = True
End If
Catch ex As Exception
flgLogin = False
strErrMsg = "Please check user/password"
End Try
IF flgLogin = True
Me.lbDisplay.Text = "Welcome " & txtUser.Text
Else
Me.lbDisplay.Text = strErrMsg
End IF
Else
flgLogin = True
End If
Catch ex As Exception
flgLogin = False
strErrMsg = "Please check user/password"
End Try
IF flgLogin = True
Me.lbDisplay.Text = "Welcome " & txtUser.Text
Else
Me.lbDisplay.Text = strErrMsg
End IF
End Sub
ยกตัวอย่างเพื่อเอาเอา class มาทำเป็น list ครับ
สร้าง class แยกออกมา Code (VB.NET)
Public NotInheritable Class PathActive
Public Shared initLDAPPath As String = "DC=opsd,DC=net"
Public Shared initLDAPServer As String = "10.0.4.32"
Public Shared initShortDomainName As String = "OPSD"
Public Shared initLDAPPath1 As String = "DC=opsd,DC=net"
Public Shared initLDAPServer1 As String = "10.0.4.1"
Public Shared initShortDomainName1 As String = "test1"
Public Shared initLDAPPath2 As String = "DC=opsd,DC=net"
Public Shared initLDAPServer2 As String = "10.0.4.2"
Public Shared initShortDomainName3 As String = "test2"
End Class
Code (VB.NET)
Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs)
strCommu = "LDAP://" & PathActive.initLDAPServer & "/" & PathActive.initLDAPPath
DomainAndUsername = PathActive.initShortDomainName & "\" & txtUser.Text
Dim entry As New DirectoryEntry(strCommu, DomainAndUsername, txtPwd.Text)
End Sub