Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 3.51 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. AD not returning the groups which authenticated user belong to
  2. Public Function ValidateActiveDirectoryLogin(ByVal domainName As String, ByVal userName As String, ByVal userPassword As String) As Boolean
  3.         Dim isValidated As Boolean = False
  4.  
  5.     Try
  6.  
  7.         Dim ldapPath As String = "LDAP://" & domainName
  8.         Dim dirEntry As New DirectoryEntry(ldapPath, userName, userPassword, AuthenticationTypes.Secure)
  9.         Dim dirSearcher As New DirectorySearcher(dirEntry)
  10.  
  11.         dirSearcher.Filter = "(SAMAccountName=" & userName & ")"
  12.         dirSearcher.PropertiesToLoad.Add("memberOf")
  13.  
  14.         Dim result As SearchResult = dirSearcher.FindOne()
  15.  
  16.         If Not result Is Nothing Then
  17.  
  18.                 For Each x As DictionaryEntry In result.Properties
  19.                     x.Key.ToString()
  20.  
  21.                     'DirectCast(x, System.Collections.DictionaryEntry).Key()
  22.                 Next
  23.  
  24.                 Dim groupCount As Integer = result.Properties("memberOf").Count
  25.                 Dim isInGroup As Boolean = False
  26.  
  27.                 For index As Integer = 0 To groupCount - 1
  28.                     Dim groupDN As String = result.Properties("memberOf").Item(index).ToString
  29.  
  30.                     Dim equalsIndex As Integer = groupDN.IndexOf("=")
  31.                     Dim commaIndex As Integer = groupDN.IndexOf(",")
  32.  
  33.                     Dim group As String = groupDN.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1).ToLower
  34.                     If group.Equals(groupName.ToLower) Then
  35.                         isInGroup = True
  36.                         Exit For
  37.                     End If
  38.                 Next index
  39.  
  40.                 isValidated = isInGroup
  41.         End If
  42.     Catch ex As Exception
  43.         Throw New Exception(ex.Message)
  44.     End Try
  45.  
  46.     Return isValidated
  47.  
  48. End Function
  49.        
  50. ` Connection to Active Directory
  51. Dim deBase As DirectoryEntry = New DirectoryEntry("LDAP://192.168.183.100:389/dc=dom,dc=fr", "jpb", "pwd")
  52.  
  53. ` Directory Search for the group your are interested in
  54. Dim dsLookForGrp As DirectorySearcher = New DirectorySearcher(deBase)
  55. dsLookForGrp.Filter = String.Format("(cn={0})", "yourgroup")
  56. dsLookForGrp.SearchScope = SearchScope.Subtree
  57. dsLookForGrp.PropertiesToLoad.Add("distinguishedName")
  58. Dim srcGrp As SearchResult = dsLookForGrp.FindOne
  59.  
  60. If (Not (srcGrp) Is Nothing) Then
  61.     Dim dsLookForUsers As DirectorySearcher = New DirectorySearcher(deBase)
  62.     dsLookForUsers.Filter = String.Format("(&(objectCategory=person)(memberOf={0}))", srcGrp.Properties("distinguishedName")(0))
  63.     dsLookForUsers.SearchScope = SearchScope.Subtree
  64.     dsLookForUsers.PropertiesToLoad.Add("objectSid")
  65.     dsLookForUsers.PropertiesToLoad.Add("userPrincipalName  ")
  66.     dsLookForUsers.PropertiesToLoad.Add("sAMAccountName")
  67.     Dim srcLstUsers As SearchResultCollection = dsLookForUsers.FindAll
  68.     For Each sruser As SearchResult In srcLstUsers
  69.         Console.WriteLine("{0}", sruser.Path)
  70.         ` Here Test if you username is insode
  71.         Console.WriteLine(""& vbTab&"{0} : {1} ", "sAMAccountName", sruser.Properties("sAMAccountName")(0))
  72.     Next
  73. End If
  74.        
  75. /* Retreiving a principal context
  76.  */
  77. Console.WriteLine("Retreiving a principal context");
  78. PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, "WM2008R2ENT:389", "dc=dom,dc=fr", "jpb", "PWD");
  79.  
  80.  
  81. /* Look for all the groups a user belongs to
  82.  */
  83. UserPrincipal aUser = UserPrincipal.FindByIdentity(domainContext, "user1");
  84. PrincipalSearchResult<Principal> a =  aUser.GetAuthorizationGroups();
  85.  
  86. foreach (GroupPrincipal gTmp in a)
  87. {
  88.   Console.WriteLine(gTmp.Name);    
  89. }