Oct 04, 2011, 7:37 PM
Return User Active Directory
Good afternoon. I have a user registration screen that works well. However I am creating a button that open a window that contains a textfield, combobox and a button. My intention in this window is to fill the combobox with the users listed in Active Directory and from the selection of a user login to fill out this user in textfield. And when I click the OK button to transfer the name, User and e-mail to the user account window.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ActiveDirectoryUsuario.aspx.cs"
Inherits="Stoque.ECM.Web.Configurador.Forms.ActiveDirectoryUsuario" %>
<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<%--Store UsuarioAD--%>
<ext:Store ID="StoreUsuarioAD" runat="server">
<Reader>
<ext:JsonReader IDProperty="nome">
<Fields>
<ext:RecordField Name="nome" />
<ext:RecordField Name="usuario" />
<ext:RecordField Name="email" />
</Fields>
</ext:JsonReader>
</Reader>
</ext:Store>
<ext:ResourceManager ID="ResourceManagerActiveDirectoryUsuario" runat="server" Theme="Gray" />
<form id="form1" runat="server">
<ext:Panel ID="WindowActiveDirectory" runat="server" Height="330" Width="283" Layout="absolute"
Frame="true">
<Items>
<ext:Panel ID="PanelActiveDirectoryNovo" runat="server" Height="350" Width="400"
Layout="absolute" X="0" Y="0">
<Items>
<ext:TextField ID="TextFieldLoginAD" runat="server" FieldLabel="Login" LabelWidth="60"
X="0" Y="0" Disabled="true" />
<ext:ComboBox ID="ComboBoxUsuario"
runat="server"
FieldLabel="Usuário"
LabelWidth="60"
X="0" Y="30"
StoreID="StoreUsuarioAD"
LoadingText="Carregando ..."
ForceSelection="true"
EmptyText="... Selecione ..."
DisplayField="nome"
ValueField="usuario"
Width="250"
AutoPostBack="true"
>
<DirectEvents>
<Select OnEvent="ComboBoxUsuario_OnValueChanged" >
<EventMask ShowMask="true" Msg="Carregando..."/>
</Select>
</DirectEvents>
</ext:ComboBox>
<ext:Button ID="ButtonOK" runat="server" Text="OK" X="5" Y="60">
<DirectEvents>
<Click OnEvent="ButtonOK_Click" />
</DirectEvents>
</ext:Button>
</Items>
</ext:Panel>
</Items>
</ext:Panel>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using Stoque.ECM.Web.Configurador.ServicoECM;
using Newtonsoft.Json;
using System.Data.Services.Client;
using Stoque.ECM.Web.Configurador.Uteis;
using Stoque.ECM.Web.Configurador.Forms;
using System.DirectoryServices.ActiveDirectory;
using System.DirectoryServices;
using System.Text;
using System.Data;
namespace Stoque.ECM.Web.Configurador.Forms
{
public partial class ActiveDirectoryUsuario : PaginaBase
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
CarregarStoreUsuarioAD();
CarregarCombo();
ComboBoxUsuario.Text = string.Empty;
}
# region Ações
protected DataTable ListarUsuariosAD()
{
string LDAP = "";
DataTable table = new DataTable("Resultados");
table.Columns.Add("Nome");
table.Columns.Add("Usuario");
table.Columns.Add("Email");
DataRow row = null;
DirectoryEntry deRoot = new DirectoryEntry(LDAP);
DirectorySearcher deSrch = new DirectorySearcher(deRoot, "(&(objectClass=user)(objectCategory=person))");
deSrch.PropertiesToLoad.Add("cn");
deSrch.PropertiesToLoad.Add("userPrincipalName");
deSrch.PropertiesToLoad.Add("sAMAccountName");
deSrch.PropertiesToLoad.Add("mail");
deSrch.Sort.PropertyName = "sAMAccountName";
foreach (SearchResult oRes in deSrch.FindAll())
{
row = table.NewRow();
row["Nome"] = oRes.Properties["cn"][0].ToString();
row["usuario"] = oRes.Properties["sAMAccountName"][0].ToString();
if (oRes.Properties.Contains("mail"))
{
row["Email"] = oRes.Properties["mail"][0].ToString();
}
table.Rows.Add(row);
}
return table;
}
protected void CarregarCombo()
{
Ext.Net.JsonReader reader = new Ext.Net.JsonReader();
DataTable usuariosAD = ListarUsuariosAD();
List<Ext.Net.ListItem> items = new List<Ext.Net.ListItem>();
int i = 1;
foreach (DataRow da in usuariosAD.Rows)
{
object[] array = da.ItemArray;
if (i == 1)
{
ComboBoxUsuario.SelectedItem.Value = array[0].ToString();
}
items.Add(new Ext.Net.ListItem(array[0].ToString()));
i++;
}
ComboBoxUsuario.Items.AddRange(items);
StoreUsuarioAD.DataSource = usuariosAD;
StoreUsuarioAD.DataBind();
this.Session["UsuariosADSistema"] = usuariosAD;
}
protected void MostrarDadosOutrosCampos()
{
if (ComboBoxUsuario.SelectedIndex > -1)
{
TextFieldLoginAD.Text = ComboBoxUsuario.SelectedItem.Text;
}
}
protected void ComboBoxUsuario_OnValueChanged(object sender, DirectEventArgs e)
{
MostrarDadosOutrosCampos();
//if (ComboBoxUsuario.SelectedIndex > -1)
//{
// this.Session["UsuarioAD"] = ComboBoxUsuario.SelectedItem.Value.ToString();
// TextFieldLoginAD.Text = ComboBoxUsuario.SelectedItem.Value.ToString();
//}
//return TextFieldLoginAD.Text;
}
protected void ButtonOK_Click(object sender, DirectEventArgs e)
{
}
#endregion Ações
#region Stores
protected void CarregarStoreUsuarioAD()
{
using (ServicoECMClient proxy = new ServicoECMClient())
{
this.Session["UsuariosADSistema"] = ListarUsuariosAD();
}
StoreUsuarioAD.DataSource = this.Session["UsuariosADSistema"];
StoreUsuarioAD.DataBind();
}
#endregion Stores
}
}