Foros del Web » Programación para mayores de 30 ;) » .NET »

[SOLUCIONADO] Lista de usuarios que pertenecen a un grupo del directorio activo

Estas en el tema de Lista de usuarios que pertenecen a un grupo del directorio activo en el foro de .NET en Foros del Web. Hola Estoy haciendo una aplicacion windows form programando en vb. necesito sacar la lista de usuarios que pertenecen a un grupo en concreto desde el ...
  #1 (permalink)  
Antiguo 23/05/2013, 01:45
 
Fecha de Ingreso: abril-2012
Mensajes: 449
Antigüedad: 12 años
Puntos: 7
Lista de usuarios que pertenecen a un grupo del directorio activo

Hola

Estoy haciendo una aplicacion windows form programando en vb. necesito sacar la lista de usuarios que pertenecen a un grupo en concreto desde el directorio activo.

¿Alguien me puede echar una mano?

__________________
Gracias por todo;

Un saludo
  #2 (permalink)  
Antiguo 23/05/2013, 06:57
 
Fecha de Ingreso: abril-2012
Mensajes: 449
Antigüedad: 12 años
Puntos: 7
Respuesta: Lista de usuarios que pertenecen a un grupo del directorio activo

Ya tengo la solución y he implementado alguna mas.
Dejo el código de toda la aplicación que he echo por si a alguien le sirve.
si tenéis alguna duda preguntar espero poder ayudaros.
Gracias;

Código vb:
Ver original
  1. Imports System.DirectoryServices.AccountManagement
  2. Imports System.DirectoryServices
  3. Imports System.Text
  4. Public Class Form1    
  5.     'Boton que devuelve la lista de usuarios de un grupo especifico
  6.    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  7.         Dim strNombreGrupo As String
  8.         Dim strUsuariosGrupo As String
  9.         strNombreGrupo = InputBox("Introduce el nombre del grupo del directorio activo " _
  10.             & "del que quieres visualizar los usuarios :", "INTRODUZCA EL GRUPO ")
  11.         strUsuariosGrupo = ObtenerUsuariosGrupo(strNombreGrupo)
  12.         MessageBox.Show(strUsuariosGrupo, "USUARIOS DEL GRUPO: " & strNombreGrupo, _
  13.             MessageBoxButtons.OK, MessageBoxIcon.Information)
  14.     End Sub
  15.  
  16.     'Funcion que devuelve toda la lista de usuarios de un grupo especifico
  17.    Public Function ObtenerUsuariosGrupo(ByVal strNombreGrupo As String) As String
  18.         Dim objDirectorioActivo As DirectoryEntry = New DirectoryEntry("LDAP://192.168.1.108/DC=indar,DC=local")
  19.         Dim dsDirectorySearcher As New DirectorySearcher(objDirectorioActivo)
  20.         Dim strUsuarios As String
  21.         Dim intEqualsIndex As Integer
  22.         Dim intCommaIndex As Integer
  23.         Dim sbGroupUsers As New StringBuilder
  24.         With dsDirectorySearcher
  25.             .Filter = "sAMAccountName=" & strNombreGrupo
  26.             .PropertiesToLoad.Add("member")
  27.             Try
  28.                 Dim dsResult As SearchResult = .FindOne
  29.                 Dim contador As Integer
  30.                 If dsResult Is Nothing Then
  31.                     Return Nothing
  32.                 End If
  33.  
  34.                 For contador = 0 To dsResult.Properties("member").Count - 1
  35.                     strUsuarios = dsResult.Properties("member")(contador).ToString
  36.                     intEqualsIndex = strUsuarios.IndexOf("=", 1)
  37.                     intCommaIndex = strUsuarios.IndexOf(",", 1)
  38.                     If intEqualsIndex = -1 Then
  39.                         Return Nothing
  40.                     End If
  41.                     'Extraer el nombre del string
  42.                    sbGroupUsers.Append(strUsuarios.Substring((intEqualsIndex + 1), (intCommaIndex - intEqualsIndex) - 1))
  43.                     sbGroupUsers.Append(ControlChars.CrLf)
  44.                 Next contador
  45.             Catch ex As Exception
  46.                 MessageBox.Show("Error en funcion ObtenerUsuariosGrupo" & vbNewLine & vbNewLine _
  47.                     & ex.Message, "Active Directory Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  48.             End Try
  49.         End With
  50.         Return sbGroupUsers.ToString
  51.     End Function
  52.  
  53.     'Boton que devuelve todos los grupos a los que pertenece un usuario especifico.
  54.    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  55.         Dim strUserLogin As String
  56.         Dim strGrupos As String
  57.         strUserLogin = InputBox("Introduzca el login name del usuario :", "INTRODUZCA EL LOGIN NAME DEL USUARIO")
  58.         strGrupos = ObtenerGruposUsuario(strUserLogin)
  59.         MessageBox.Show(strGrupos, "GRUPOS DEL USUARIO: " & strUserLogin, MessageBoxButtons.OK, MessageBoxIcon.Information)
  60.     End Sub
  61.  
  62.     'Devuelve todos los grupos a los que pertenece un usuario especifico
  63.    Public Function ObtenerGruposUsuario(ByVal strLoginName As String) As String
  64.         Dim dsDirectoryEntry As New DirectoryEntry("LDAP://192.168.1.108/DC=indar,DC=local")
  65.         Dim dsSearcher As New DirectorySearcher(dsDirectoryEntry)
  66.         Dim contador As Integer
  67.         Dim strGrupos As String
  68.         Dim intEqualsIndex As Integer
  69.         Dim intCommaIndex As Integer
  70.         Dim sbUserGroups As New StringBuilder
  71.         dsSearcher.Filter = "saMAccountName=" & strLoginName
  72.         dsSearcher.PropertiesToLoad.Add("memberOf")
  73.         Try
  74.             Dim dsResult As SearchResult = dsSearcher.FindOne
  75.             If dsResult Is Nothing Then
  76.                 Return Nothing
  77.             End If
  78.             For contador = 0 To (dsResult.Properties("memberOf").Count - 1)
  79.                 strGrupos = CType(dsResult.Properties("memberOf")(contador), String)
  80.                 intEqualsIndex = strGrupos.IndexOf("=", 1)
  81.                 intCommaIndex = strGrupos.IndexOf(",", 1)
  82.                 If intEqualsIndex = -1 Then
  83.                     Return Nothing
  84.                 End If
  85.                 sbUserGroups.Append(strGrupos.Substring((intEqualsIndex + 1), (intCommaIndex - intEqualsIndex) - 1))
  86.                 sbUserGroups.Append(ControlChars.CrLf)
  87.             Next
  88.         Catch ex As Exception
  89.             MessageBox.Show("Error en funcion ObtenerGruposUsuario" & vbNewLine & vbNewLine _
  90.                 & ex.Message, "Active Directory Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  91.         End Try
  92.         Return sbUserGroups.ToString.Trim
  93.     End Function
  94.  
  95.     'Boton con el que se comprueba si un usuario pertenece a un grupo
  96.    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  97.         Dim strLogin As String
  98.         Dim strGrupo As String
  99.         Dim blnValidated As Boolean
  100.         Dim strDisplayResults As String
  101.         strLogin = InputBox("Introduce el LOGIN name del usuario: ", "INTRODUZCA LOGIN NAME DEL USUARIO")
  102.         strGrupo = InputBox("Introduce el GRUPO: ", "INTRODUZCA EL GRUPO")
  103.         blnValidated = ValidarGrupoUsuario(strLogin, strGrupo)
  104.         If blnValidated = True Then
  105.             strDisplayResults = "El usuario '" & strLogin & "' SI pertenece al grupo '" & strGrupo & "' ."
  106.         Else
  107.             strDisplayResults = "El usuario '" & strLogin & "' NO pertenece al grupo '" & strGrupo & "' ."
  108.         End If
  109.         MessageBox.Show(strDisplayResults, "Comprobar si un USUARIO pertenece a un GRUPO", _
  110.             MessageBoxButtons.OK, MessageBoxIcon.Information)
  111.     End Sub
  112.  
  113.     'Funcion que devuelve TRUE si el usuario pertenece al grupo y devuelve FALSE si el usuario no pertenece al grupo
  114.    Public Function ValidarGrupoUsuario(ByVal strLoginName As String, ByVal strNombreGrupo As String) As Boolean
  115.         Dim strNombreUsuario As String
  116.         Dim strUsuariosGrupo As String
  117.         Dim blnUserValidated As Boolean = False
  118.         Dim intResult As Integer
  119.         strNombreUsuario = ObtenerNombreUsuario(strLoginName)
  120.         If strNombreUsuario.Trim = "" Then
  121.             Return blnUserValidated
  122.         End If
  123.         strUsuariosGrupo = ObtenerUsuariosGrupo(strNombreGrupo)
  124.         If strUsuariosGrupo = Nothing Then
  125.             Return blnUserValidated
  126.         End If
  127.         intResult = strUsuariosGrupo.IndexOf(strNombreUsuario)
  128.         If intResult > -1 Then
  129.             'User found in group
  130.            blnUserValidated = True
  131.         Else
  132.             'User not found in group
  133.            blnUserValidated = False
  134.         End If
  135.         Return blnUserValidated
  136.     End Function
  137.  
  138.     'Devuelve el nombre completo del directorio activo
  139.    Public Function ObtenerNombreUsuario(Optional ByVal strLoginName As String = "", Optional ByVal strDomainName As String = "") As String
  140.         Dim dsDirectoryEntry As New DirectoryEntry
  141.         Dim strPath As String
  142.         Dim strDomain As String
  143.         Dim strLogin As String
  144.         Dim strFullName As String
  145.         If strDomainName = "" Then
  146.             strDomain = GetDomainName()
  147.         Else
  148.             strDomain = strDomainName
  149.         End If
  150.         If strLoginName = "" Then
  151.             strLogin = GetLoginName()
  152.         Else
  153.             strLogin = strLoginName
  154.         End If
  155.         'Unknown error when trying to use LADP instead of WinNT
  156.        strPath = "WinNT://" & strDomain & "/" & strLogin
  157.         Try
  158.             dsDirectoryEntry = New DirectoryEntry(strPath)
  159.             strFullName = CType(dsDirectoryEntry.Invoke("Get", "FullName"), String)
  160.         Catch ex As Exception
  161.             strFullName = ""
  162.         Finally
  163.             dsDirectoryEntry.Close()
  164.             dsDirectoryEntry.Dispose()
  165.         End Try
  166.         Return strFullName
  167.     End Function
  168.  
  169.     Public Function GetDomainName() As String
  170.         Dim strDomain As String
  171.         strDomain = System.Security.Principal.WindowsIdentity.GetCurrent.Name
  172.         strDomain = Mid(strDomain, 1, InStr(strDomain, "\") - 1)
  173.         Return strDomain
  174.     End Function
  175.  
  176.     Public Function GetLoginName() As String
  177.         'Returns the users Login name
  178.        Return System.Environment.UserName
  179.     End Function
  180.  
  181.     Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
  182.         Me.Close()
  183.     End Sub
  184. End Class

__________________
Gracias por todo;

Un saludo

Etiquetas: activo, directorio, grupo, lista, usuarios, vb
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 00:20.