2017-06-02 35 views
0

以下是用於檢查輸入的信用是否良好的方法。我也想補充一下,看看他們是否屬於「XXX」組。檢查登錄憑據以查看它們在Active Directory中是否有效並檢查它們是否與AD中的特定組相區別

Private Function ValidateActiveDirectoryLogin(ByVal Domain As String, ByVal Username As String, ByVal Password As String) As Boolean 
    Dim Success As Boolean = False 
    Dim Entry As New System.DirectoryServices.DirectoryEntry("LDAP://" + Domain, Username, Password) 
    Dim Searcher As New System.DirectoryServices.DirectorySearcher(Entry) 
    Searcher.SearchScope = DirectoryServices.SearchScope.OneLevel 
    Try 
     Dim Results As System.DirectoryServices.SearchResult = Searcher.FindOne 
     Success = Not (Results Is Nothing) 
    Catch ex As Exception 
     Success = False 
    End Try 
    Return Success 

End Function 

和下面我試圖玩弄的東西,我發現了堆,但我不是運氣不好。我如何使用現有的方法並添加到它以獲得我的結果?

Public Function IsInGroup(ByVal UserName As String) As Boolean 
    'Dim MyIdentity As System.Security.Principal.WindowsIdentity = New WindowsPrincipal(New WindowsIdentity(UserName)) ' System.Security.Principal.WindowsIdentity.GetCurrent() 
    'Dim userPrincipal = New WindowsPrincipal(New WindowsIdentity(Username)) 
    Dim MyPrincipal As System.Security.Principal.WindowsPrincipal = New WindowsPrincipal(New WindowsIdentity(UserName)) 'New System.Security.Principal.WindowsPrincipal(userPrincipal) 
    Return MyPrincipal.IsInRole("XXX_YYY") 
End Function 

也試圖做這樣的事情,但得到錯誤我截圖。

Public Function IsInGroup(ByVal UserName As String) As Boolean 
    Dim Result As Boolean 
    Dim de As New DirectoryEntry("LDAP://AD") 
    Dim MemberSearcher As New DirectorySearcher 

    With MemberSearcher 
     .SearchRoot = de 
     .Filter = "(&(ObjectClass=Group)(CN=VAL_ITS))" 
     .PropertiesToLoad.Add("Member") 
    End With 

    Dim mySearchResults As SearchResult = MemberSearcher.FindOne() 

    For Each User In mySearchResults.Properties("Member") 

     If User = UserName Then 
      Result = True 
     Else 
      Result = False 
     End If 
    Next 

    Return Result 
End Function 

enter image description here

回答

0
'Project > Add Reference > System.DirectoryServices.AccountManagement & System.DirectoryServices 

驗證使用System.DirectoryServices.AccountManagement命名空間

Imports System.DirectoryServices.AccountManagement 

    Public function validate(username as string, password as string, domain as string) 

     Dim valid As Boolean = False 

     Using context As New PrincipalContext(ContextType.Domain, domain) 
      valid = context.ValidateCredentials(username, password) 
     End Using 

     return valid 

    End Function 

    Public function checkgroup(domain as string, username as string, groupname as string) 

     Dim isMember as boolean = false 
     Dim ctx As New PrincipalContext(ContextType.Domain, domain) 
     Dim user As UserPrincipal = UserPrincipal.FindByIdentity(ctx, username) 
     Dim group As GroupPrincipal = GroupPrincipal.FindByIdentity(ctx, groupname) 

     If user IsNot Nothing Then 
     If user.IsMemberOf(group) Then 
      isMember = True 
     End If 
     End If 

     return isMember 

    End Function 
+0

林進口的System.DirectoryServices但是我沒有得到 「PrincipalContext」 來定義自身。並且它不識別AccountManagement – Gio

+0

添加對System.DirectoryServices.AccountManagement的引用,然後導入System.DirectoryServices.AccountManagement – TonyW

相關問題