Visual Basic 和活动目录
Visual Basic and Active Directory
我在以下代码中遇到错误:
Private Function AuthenticateUser() As Boolean
Dim username As String = txtbok_login_username.Text
Dim password As String = txtbox_login_password.Text
Dim domain As String = "domain.local"
Dim isAuthenticated As Boolean = ValidateActiveDirectoryLogin(domain, username, password, "Admins@WokasCustomer.com")
Return isAuthenticated
End Function
Public Function ValidateActiveDirectoryLogin(ByVal domainName As String, ByVal userName As String, ByVal userPassword As String, ByVal groupName As String) As Boolean
Dim isValidated As Boolean = False
Try
Dim ldapPath As String = "LDAP://domain.local"
Dim dirEntry As New DirectoryServices.DirectoryEntries(ldapPath, userName, password, authenticationtypes.secure)
Dim dirSearcher As New DirectoryServices.DirectorySearcher(dirEntry)
dirSearcher.Filter = "(userPrincipalName=" & userName & ")"
dirSearcher.PropertiesToLoad.Add("memberOf")
Dim result As DirectoryServices.SearchResult = dirSearcher.FindOne()
If Not result Is Nothing Then
If groupName.Length = 0 Then
isValidated = True
Else
Dim groupCount As Integer = result.Properties("Fiserv Processing - MIS").Count
Dim isInGroup As Boolean = False
For index As Integer = 0 To groupCount - 1
Dim groupDN As String = result.Properties("Fiserv Processing - MIS").Item(index)
Dim equalsIndex As Integer = groupDN.IndexOf("=")
Dim commaIndex As Integer = groupDN.IndexOf(",")
Dim group As String = groupDN.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1).ToLower
If group.Equals(groupName.ToLower) Then
isInGroup = True
Exit For
End If
Next index
isValidated = isInGroup
End If
End If
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
Return isValidated
End Function
错误码如下:
Error 2 Overload resolution failed because no accessible 'New' can be called with these arguments:
'Public Sub New(filter As String)': Value of type 'System.DirectoryServices.DirectoryEntries' cannot be converted to
'String'.
'Public Sub New(searchRoot As System.DirectoryServices.DirectoryEntry)': Value of type
'System.DirectoryServices.DirectoryEntries' cannot be converted to
'System.DirectoryServices.DirectoryEntry'.
和
Error 1 Type 'System.DirectoryServices.DirectoryEntries' has no constructors.
我的目标是让 AD 身份验证检查用户是否是特定 AD 组的成员。
如有任何帮助,我们将不胜感激。
您的 dirEntries
变量似乎使用 DirectoryEntries
class 而不是 DirectoryEntry
。 DirectorySearcher
没有允许它使用 DirectoryEntries
对象(它是 DirectoryEntry
对象的集合)的构造函数。
查看您正在使用的 classes 的文档。
https://msdn.microsoft.com/en-us/library/system.directoryservices.directorysearcher(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.directoryservices.directoryentries(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.directoryservices.directoryentry(v=vs.110).aspx
Dim dirEntry As New DirectoryServices.DirectoryEntries(ldapPath, userName, password, authenticationtypes.secure)
Dim dirSearcher As New DirectoryServices.DirectorySearcher(dirEntry)
应该是
Dim dirEntry As New DirectoryServices.DirectoryEntry(ldapPath, userName, password, authenticationtypes.secure)
Dim dirSearcher As New DirectoryServices.DirectorySearcher(dirEntry)
我相信您的 dirEntry 声明应该如下所示:
Dim dirEntry As New DirectoryServices.DirectoryEntry(ldapPath, userName, userPassword, DirectoryServices.AuthenticationTypes.Secure)
修改后的全部代码如下
Public Class form_login
Private Function AuthenticateUser() As Boolean
Dim username As String = txtbok_login_username.Text
Dim password As String = txtbox_login_password.Text
Dim domain As String = "patten.local"
Dim isAuthenticated As Boolean = ValidateActiveDirectoryLogin(domain, username, password)
Return isAuthenticated
End Function
Public Function ValidateActiveDirectoryLogin(ByVal domainName As String, ByVal userName As String, ByVal userPassword As String, ByVal groupName As String) As Boolean
Dim isValidated As Boolean = False
Try
Dim ldapPath As String = "LDAP://patten.local"
Dim dirEntry As New DirectoryServices.DirectoryEntry(ldapPath, userName, userPassword, DirectoryServices.AuthenticationTypes.Secure)
Dim dirSearcher As New DirectoryServices.DirectorySearcher(dirEntry)
dirSearcher.Filter = "(userPrincipalName=" & userName & ")"
dirSearcher.PropertiesToLoad.Add("memberOf")
Dim result As DirectoryServices.SearchResult = dirSearcher.FindOne()
If Not result Is Nothing Then
If groupName.Length = 0 Then
isValidated = True
Else
Dim groupCount As Integer = result.Properties("Fiserv Processing - MIS").Count
Dim isInGroup As Boolean = False
For index As Integer = 0 To groupCount - 1
Dim groupDN As String = result.Properties("Fiserv Processing - MIS").Item(index)
Dim equalsIndex As Integer = groupDN.IndexOf("=")
Dim commaIndex As Integer = groupDN.IndexOf(",")
Dim group As String = groupDN.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1).ToLower
If group.Equals(groupName.ToLower) Then
isInGroup = True
Exit For
End If
Next index
isValidated = isInGroup
End If
End If
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
Return isValidated
End Function
Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
Me.Close()
End Sub
Private Sub UsernameLabel_Click(sender As Object, e As EventArgs) Handles UsernameLabel.Click
End Sub
Private Sub form_login_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
结束Class
现在单击“确定”后登录表单不会继续。代码是否可能正在扫描活动目录并需要很长时间才能进行身份验证?或者更有可能是我的 post 身份验证后的操作代码编码不正确? –
我在以下代码中遇到错误:
Private Function AuthenticateUser() As Boolean
Dim username As String = txtbok_login_username.Text
Dim password As String = txtbox_login_password.Text
Dim domain As String = "domain.local"
Dim isAuthenticated As Boolean = ValidateActiveDirectoryLogin(domain, username, password, "Admins@WokasCustomer.com")
Return isAuthenticated
End Function
Public Function ValidateActiveDirectoryLogin(ByVal domainName As String, ByVal userName As String, ByVal userPassword As String, ByVal groupName As String) As Boolean
Dim isValidated As Boolean = False
Try
Dim ldapPath As String = "LDAP://domain.local"
Dim dirEntry As New DirectoryServices.DirectoryEntries(ldapPath, userName, password, authenticationtypes.secure)
Dim dirSearcher As New DirectoryServices.DirectorySearcher(dirEntry)
dirSearcher.Filter = "(userPrincipalName=" & userName & ")"
dirSearcher.PropertiesToLoad.Add("memberOf")
Dim result As DirectoryServices.SearchResult = dirSearcher.FindOne()
If Not result Is Nothing Then
If groupName.Length = 0 Then
isValidated = True
Else
Dim groupCount As Integer = result.Properties("Fiserv Processing - MIS").Count
Dim isInGroup As Boolean = False
For index As Integer = 0 To groupCount - 1
Dim groupDN As String = result.Properties("Fiserv Processing - MIS").Item(index)
Dim equalsIndex As Integer = groupDN.IndexOf("=")
Dim commaIndex As Integer = groupDN.IndexOf(",")
Dim group As String = groupDN.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1).ToLower
If group.Equals(groupName.ToLower) Then
isInGroup = True
Exit For
End If
Next index
isValidated = isInGroup
End If
End If
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
Return isValidated
End Function
错误码如下:
Error 2 Overload resolution failed because no accessible 'New' can be called with these arguments: 'Public Sub New(filter As String)': Value of type 'System.DirectoryServices.DirectoryEntries' cannot be converted to 'String'. 'Public Sub New(searchRoot As System.DirectoryServices.DirectoryEntry)': Value of type 'System.DirectoryServices.DirectoryEntries' cannot be converted to 'System.DirectoryServices.DirectoryEntry'.
和
Error 1 Type 'System.DirectoryServices.DirectoryEntries' has no constructors.
我的目标是让 AD 身份验证检查用户是否是特定 AD 组的成员。
如有任何帮助,我们将不胜感激。
您的 dirEntries
变量似乎使用 DirectoryEntries
class 而不是 DirectoryEntry
。 DirectorySearcher
没有允许它使用 DirectoryEntries
对象(它是 DirectoryEntry
对象的集合)的构造函数。
查看您正在使用的 classes 的文档。
https://msdn.microsoft.com/en-us/library/system.directoryservices.directorysearcher(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.directoryservices.directoryentries(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.directoryservices.directoryentry(v=vs.110).aspx
Dim dirEntry As New DirectoryServices.DirectoryEntries(ldapPath, userName, password, authenticationtypes.secure)
Dim dirSearcher As New DirectoryServices.DirectorySearcher(dirEntry)
应该是
Dim dirEntry As New DirectoryServices.DirectoryEntry(ldapPath, userName, password, authenticationtypes.secure)
Dim dirSearcher As New DirectoryServices.DirectorySearcher(dirEntry)
我相信您的 dirEntry 声明应该如下所示:
Dim dirEntry As New DirectoryServices.DirectoryEntry(ldapPath, userName, userPassword, DirectoryServices.AuthenticationTypes.Secure)
修改后的全部代码如下
Public Class form_login
Private Function AuthenticateUser() As Boolean
Dim username As String = txtbok_login_username.Text
Dim password As String = txtbox_login_password.Text
Dim domain As String = "patten.local"
Dim isAuthenticated As Boolean = ValidateActiveDirectoryLogin(domain, username, password)
Return isAuthenticated
End Function
Public Function ValidateActiveDirectoryLogin(ByVal domainName As String, ByVal userName As String, ByVal userPassword As String, ByVal groupName As String) As Boolean
Dim isValidated As Boolean = False
Try
Dim ldapPath As String = "LDAP://patten.local"
Dim dirEntry As New DirectoryServices.DirectoryEntry(ldapPath, userName, userPassword, DirectoryServices.AuthenticationTypes.Secure)
Dim dirSearcher As New DirectoryServices.DirectorySearcher(dirEntry)
dirSearcher.Filter = "(userPrincipalName=" & userName & ")"
dirSearcher.PropertiesToLoad.Add("memberOf")
Dim result As DirectoryServices.SearchResult = dirSearcher.FindOne()
If Not result Is Nothing Then
If groupName.Length = 0 Then
isValidated = True
Else
Dim groupCount As Integer = result.Properties("Fiserv Processing - MIS").Count
Dim isInGroup As Boolean = False
For index As Integer = 0 To groupCount - 1
Dim groupDN As String = result.Properties("Fiserv Processing - MIS").Item(index)
Dim equalsIndex As Integer = groupDN.IndexOf("=")
Dim commaIndex As Integer = groupDN.IndexOf(",")
Dim group As String = groupDN.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1).ToLower
If group.Equals(groupName.ToLower) Then
isInGroup = True
Exit For
End If
Next index
isValidated = isInGroup
End If
End If
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
Return isValidated
End Function
Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
Me.Close()
End Sub
Private Sub UsernameLabel_Click(sender As Object, e As EventArgs) Handles UsernameLabel.Click
End Sub
Private Sub form_login_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
结束Class
现在单击“确定”后登录表单不会继续。代码是否可能正在扫描活动目录并需要很长时间才能进行身份验证?或者更有可能是我的 post 身份验证后的操作代码编码不正确? –