AD 未返回经过身份验证的用户所属的组

发布于 2024-12-27 10:33:23 字数 1879 浏览 1 评论 0原文

我能够使用 LDAP 验证给定用户 - 域、用户名和密码,但无法检索他与之关联的组:(

这里是我正在使用的代码,

Public Function ValidateActiveDirectoryLogin(ByVal domainName As String, ByVal userName As String, ByVal userPassword As String) As Boolean
        Dim isValidated As Boolean = False

    Try

        Dim ldapPath As String = "LDAP://" & domainName
        Dim dirEntry As New DirectoryEntry(ldapPath, userName, userPassword, AuthenticationTypes.Secure)
        Dim dirSearcher As New DirectorySearcher(dirEntry)

        dirSearcher.Filter = "(SAMAccountName=" & userName & ")"
        dirSearcher.PropertiesToLoad.Add("memberOf")

        Dim result As SearchResult = dirSearcher.FindOne()

        If Not result Is Nothing Then

                For Each x As DictionaryEntry In result.Properties
                    x.Key.ToString()

                    'DirectCast(x, System.Collections.DictionaryEntry).Key()
                Next

                Dim groupCount As Integer = result.Properties("memberOf").Count
                Dim isInGroup As Boolean = False

                For index As Integer = 0 To groupCount - 1
                    Dim groupDN As String = result.Properties("memberOf").Item(index).ToString

                    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
    Catch ex As Exception
        Throw New Exception(ex.Message)
    End Try

    Return isValidated

End Function

请帮助...

Venky

I'm able to authenticate given user - Domain, UserName and Password with LDAP but not able to retrive his groups which he associated with :(

Here the code i'm using

Public Function ValidateActiveDirectoryLogin(ByVal domainName As String, ByVal userName As String, ByVal userPassword As String) As Boolean
        Dim isValidated As Boolean = False

    Try

        Dim ldapPath As String = "LDAP://" & domainName
        Dim dirEntry As New DirectoryEntry(ldapPath, userName, userPassword, AuthenticationTypes.Secure)
        Dim dirSearcher As New DirectorySearcher(dirEntry)

        dirSearcher.Filter = "(SAMAccountName=" & userName & ")"
        dirSearcher.PropertiesToLoad.Add("memberOf")

        Dim result As SearchResult = dirSearcher.FindOne()

        If Not result Is Nothing Then

                For Each x As DictionaryEntry In result.Properties
                    x.Key.ToString()

                    'DirectCast(x, System.Collections.DictionaryEntry).Key()
                Next

                Dim groupCount As Integer = result.Properties("memberOf").Count
                Dim isInGroup As Boolean = False

                For index As Integer = 0 To groupCount - 1
                    Dim groupDN As String = result.Properties("memberOf").Item(index).ToString

                    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
    Catch ex As Exception
        Throw New Exception(ex.Message)
    End Try

    Return isValidated

End Function

Please help...

Venky

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枫以 2025-01-03 10:33:23

这是我将使用的方法,抱歉,这是我从 C# 转换为 VB.Net 的代码。

` Connection to Active Directory
Dim deBase As DirectoryEntry = New DirectoryEntry("LDAP://192.168.183.100:389/dc=dom,dc=fr", "jpb", "pwd")

` Directory Search for the group your are interested in
Dim dsLookForGrp As DirectorySearcher = New DirectorySearcher(deBase)
dsLookForGrp.Filter = String.Format("(cn={0})", "yourgroup")
dsLookForGrp.SearchScope = SearchScope.Subtree
dsLookForGrp.PropertiesToLoad.Add("distinguishedName")
Dim srcGrp As SearchResult = dsLookForGrp.FindOne

If (Not (srcGrp) Is Nothing) Then
    Dim dsLookForUsers As DirectorySearcher = New DirectorySearcher(deBase)
    dsLookForUsers.Filter = String.Format("(&(objectCategory=person)(memberOf={0}))", srcGrp.Properties("distinguishedName")(0))
    dsLookForUsers.SearchScope = SearchScope.Subtree
    dsLookForUsers.PropertiesToLoad.Add("objectSid")
    dsLookForUsers.PropertiesToLoad.Add("userPrincipalName  ")
    dsLookForUsers.PropertiesToLoad.Add("sAMAccountName")
    Dim srcLstUsers As SearchResultCollection = dsLookForUsers.FindAll
    For Each sruser As SearchResult In srcLstUsers
        Console.WriteLine("{0}", sruser.Path)
        ` Here Test if you username is insode 
        Console.WriteLine(""& vbTab&"{0} : {1} ", "sAMAccountName", sruser.Properties("sAMAccountName")(0))
    Next
End If

请注意,主要组由 primaryGroupID 给出,它不是 DN,而是 ID,它是 lasr 部分组 SID。

最后一件事,但您也可以使用 在 .NET Framework 中管理目录安全主体来完成此操作3.5。这是 C# 中的示例

/* Retreiving a principal context
 */
Console.WriteLine("Retreiving a principal context");
PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, "WM2008R2ENT:389", "dc=dom,dc=fr", "jpb", "PWD");


/* Look for all the groups a user belongs to
 */
UserPrincipal aUser = UserPrincipal.FindByIdentity(domainContext, "user1");
PrincipalSearchResult<Principal> a =  aUser.GetAuthorizationGroups();

foreach (GroupPrincipal gTmp in a)
{
  Console.WriteLine(gTmp.Name);    
}

Here is the way I will use, sorry it's code I translate from C# to VB.Net

` Connection to Active Directory
Dim deBase As DirectoryEntry = New DirectoryEntry("LDAP://192.168.183.100:389/dc=dom,dc=fr", "jpb", "pwd")

` Directory Search for the group your are interested in
Dim dsLookForGrp As DirectorySearcher = New DirectorySearcher(deBase)
dsLookForGrp.Filter = String.Format("(cn={0})", "yourgroup")
dsLookForGrp.SearchScope = SearchScope.Subtree
dsLookForGrp.PropertiesToLoad.Add("distinguishedName")
Dim srcGrp As SearchResult = dsLookForGrp.FindOne

If (Not (srcGrp) Is Nothing) Then
    Dim dsLookForUsers As DirectorySearcher = New DirectorySearcher(deBase)
    dsLookForUsers.Filter = String.Format("(&(objectCategory=person)(memberOf={0}))", srcGrp.Properties("distinguishedName")(0))
    dsLookForUsers.SearchScope = SearchScope.Subtree
    dsLookForUsers.PropertiesToLoad.Add("objectSid")
    dsLookForUsers.PropertiesToLoad.Add("userPrincipalName  ")
    dsLookForUsers.PropertiesToLoad.Add("sAMAccountName")
    Dim srcLstUsers As SearchResultCollection = dsLookForUsers.FindAll
    For Each sruser As SearchResult In srcLstUsers
        Console.WriteLine("{0}", sruser.Path)
        ` Here Test if you username is insode 
        Console.WriteLine(""& vbTab&"{0} : {1} ", "sAMAccountName", sruser.Properties("sAMAccountName")(0))
    Next
End If

Be careful the primary group is given by the primaryGroupID and it's not a DN but an ID which is the lasr part of the group SID.

Last thing, But you can also do it using Managing Directory Security Principals in the .NET Framework 3.5. Here is a sample in C#

/* Retreiving a principal context
 */
Console.WriteLine("Retreiving a principal context");
PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, "WM2008R2ENT:389", "dc=dom,dc=fr", "jpb", "PWD");


/* Look for all the groups a user belongs to
 */
UserPrincipal aUser = UserPrincipal.FindByIdentity(domainContext, "user1");
PrincipalSearchResult<Principal> a =  aUser.GetAuthorizationGroups();

foreach (GroupPrincipal gTmp in a)
{
  Console.WriteLine(gTmp.Name);    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文