コード例 #1
0
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Convert.ToBoolean(ConfigurationManager.AppSettings["EntornoLocal"]))
        {
            usuario.Nombre   = "Juan";
            usuario.Apellido = "Peréz";

            usuario.NombreMostrable = usuario.Nombre.ToUpper() + " " + usuario.Apellido.ToUpper();
        }
        else
        {
            usuario = ActiveDirectory.obtieneUsuario();
        }

        string searchTerm = Request.QueryString["search"];

        search.Value = searchTerm;

        SqlConnection con = new SqlConnection(this.ReturnConnectionString());

        con.Open();

        SqlCommand cmd = new SqlCommand("Buscar_Personal_SEL", con);

        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@Buscar", SqlDbType.NVarChar, 50).Value = searchTerm;
        cmd.Connection = con;

        SqlDataReader dr = cmd.ExecuteReader();

        if (dr.HasRows && !String.IsNullOrEmpty(searchTerm))
        {
            results.Visible      = true;
            Repeater1.DataSource = dr;
            Repeater1.DataBind();
        }
        else if (!String.IsNullOrEmpty(searchTerm))
        {
            noResults.Visible = true;
        }

        cmd.Connection = con;
        con.Close();
    }
コード例 #2
0
        public static UsuarioAd obtieneUsuario()
        {
            var nombreUsuario = System.Web.HttpContext.Current.Request.LogonUserIdentity.Name;

            nombreUsuario = nombreUsuario.Replace("CNV\\", "");
            // var ctx = new PrincipalContext(ContextType.Domain);
            //var user = UserPrincipal.Current;
            //var nombreUsuario = user.DisplayName;
            //var nombre2 = System.Web.HttpContext.Current.User.Identity.Name.ToString();

            // Lista para Retornar
            var lList = new List <Entidades.UsuarioAd>();

            // Utiliza las credenciales de Login de Win.
            // Using rootDE As DirectoryEntry = Domain.GetCurrentDomain().GetDirectoryEntry() ' Busca Usuarios desde la raíz del Active Directory
            // Using rootDE As New DirectoryEntry("LDAP://cnv.gov.ar/CN=Domain Users,OU=UsuariosCNV,DC=cnv,DC=gov,DC=ar")

            // using (var rootDE = new DirectoryEntry("LDAP://ou=UsuariosCNV,dc=cnv,dc=gov,dc=ar"))
            // using (var rootDE = Domain.GetCurrentDomain().GetDirectoryEntry())
            // using (var rootDE = new DirectoryEntry("LDAP://ou=UsuariosCNV,dc=cnv,dc=gov,dc=ar"))
            using (var rootDE = new DirectoryEntry("LDAP://ou=UsuariosCNV,dc=cnv,dc=gov,dc=ar"))
            {
                // Crea una instancia de DirectorySearcher que permite buscar en el rootDE definido arriba
                using (var dSearcher = new DirectorySearcher(rootDE))
                {
                    // Verifica si se quieren listar todos los usuarios o uno especifico pasado por param
                    if (nombreUsuario == "")
                    {
                        // Define el filtro de busqueda del objeto principal en el AD
                        dSearcher.Filter = "(&(objectCategory=Person)(objectClass=User))";
                    }
                    else
                    {
                        dSearcher.Filter =
                            $"(&(objectCategory=Person)(objectClass=User)(!(userAccountControl:1.2.840.113556.1.4.803:=2))(sAMAccountName={"*" + nombreUsuario + "*"}))";
                    }

                    // Cargamos el dSearcher con las propiedades del objeto que queremos obtener
                    dSearcher.PropertiesToLoad.Add("sAMAccountName"); //cuenta
                    dSearcher.PropertiesToLoad.Add("giveName");       //nombreDePila
                    dSearcher.PropertiesToLoad.Add("sn");             //apellidos
                    dSearcher.PropertiesToLoad.Add("displayName");    //nombreParaMostrar
                    dSearcher.PropertiesToLoad.Add("description");    //descripcion
                    dSearcher.PropertiesToLoad.Add("mail");           //correoElectronico
                    dSearcher.PropertiesToLoad.Add("company");        //organizacion  ej. Gcia Servicios Centrales
                    dSearcher.PropertiesToLoad.Add("department");     //departamento ej. Subgcia de Informatica y Organizacion
                    dSearcher.PropertiesToLoad.Add("title");          //puesto ej. Empleado

                    // Definimos un colección para contener todos los resultados de busqueda
                    var sResult = dSearcher.FindAll();

                    if (sResult.Count == 0)
                    {
                        throw new System.Exception(

                                  "Error en la Función dlusuariosActiveDirectory.listaUsuarios. No se encontraron las propiedades company, department, title, etc <br/>");
                    }

                    foreach (SearchResult result in sResult)
                    {
                        if (result.Properties.Contains("mail"))
                        {
                            var oEntUsuarios = new UsuarioAd();

                            try
                            {
                                if (result.Properties.Contains("sAMAccountName"))
                                {
                                    oEntUsuarios.Cuenta = Convert.ToString(result.Properties["sAMAccountName"][0]);
                                }

                                if (result.Properties.Contains("giveName"))
                                {
                                    oEntUsuarios.Nombre = Convert.ToString(result.Properties["giveName"][0]);
                                }

                                if (result.Properties.Contains("sn"))
                                {
                                    oEntUsuarios.Apellido = Convert.ToString(result.Properties["sn"][0]);
                                }

                                if (result.Properties.Contains("displayName"))
                                {
                                    oEntUsuarios.NombreMostrable = Convert.ToString(result.Properties["displayName"][0]);
                                }

                                if (result.Properties.Contains("description"))
                                {
                                    oEntUsuarios.Descripcion = Convert.ToString(result.Properties["description"][0]);
                                }

                                if (result.Properties.Contains("mail"))
                                {
                                    oEntUsuarios.CorreoElectronico = Convert.ToString(result.Properties["mail"][0]);
                                }

                                if (result.Properties.Contains("company"))
                                {
                                    oEntUsuarios.Organizacion = Convert.ToString(result.Properties["company"][0]);
                                }

                                if (result.Properties.Contains("department"))
                                {
                                    oEntUsuarios.Departamento = Convert.ToString(result.Properties["department"][0]);
                                }

                                if (result.Properties.Contains("title"))
                                {
                                    oEntUsuarios.Puesto = Convert.ToString(result.Properties["title"][0]);
                                }
                            }
                            catch (Exception e)
                            {
                                throw new System.Exception("Error en listaUsuarios (no hay propiedades): " +
                                                           e.Message + "<br>" + "Stack Trace: " + e.StackTrace);
                            }
                            lList.Add(oEntUsuarios);
                        }
                    }
                }
            }

            return(lList[0]);
        }