1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.DirectoryServices;
using AD;
namespace ActiveDirectory
{
public partial class AddUser : Form
{
//DirectoryEntry deEntry = new DirectoryEntry("LDAP://OU=ELEVES,OU=IS3,OU=PARIS,DC=supinfo,DC=com", "Login", "Password");
public AddUser()
{
InitializeComponent();
this.tbGivenName.Text = "Juju";
this.tbInitials.Text = "JG";
this.tbSurname.Text = "Gari";
this.tbSameAccountName.Text = "toto_as3";
this.tbPassword.Text = "Azerty123";
this.tbMail.Text = "totoas3@channel.fr";
LoadOrganizationalnits();
LoadGroups();
}
private void btnClose_Click(object sender, EventArgs e)
{
Dispose();
}
private void btnAddEntry_Click(object sender, EventArgs e)
{
this.btnAddEntry.Text = "Processing";
this.btnAddEntry.Enabled = false;
UserDetails ud = new UserDetails(this.tbGivenName.Text, this.tbSurname.Text,
this.tbInitials.Text, this.tbCommonName.Text, this.tbMail.Text,
this.tbSameAccountName.Text, this.tbPassword.Text, this.cbOu.SelectedItem.ToString());
//TODO : ajouter l'entrée dans le Ldap courant
try
{
using (DirectoryEntry userEntry = GetPath(cbOu.SelectedItem.ToString()))
{
object conn = userEntry.NativeObject;
ADActions adActions = new ADActions();
adActions.AddUserToActiveDirectory(ud, this.cbGroups.SelectedItem.ToString(), userEntry);
this.lblMessage.Text = "The creation of user " + ud.GIVEN_NAME + " " + ud.SN + " has been done successfully.";
this.lblMessage.ForeColor = Color.Blue;
}
}
catch (Exception exc)
{
this.lblMessage.Text = exc.Message +
":\r\n" +
exc.InnerException.Message + "\r\n";
this.lblMessage.ForeColor = Color.Red;
}
this.btnAddEntry.Text = "Add Entry";
this.btnAddEntry.Enabled = true;
}
private void LoadOrganizationalnits()
{
DirectoryEntry orgEntries = new DirectoryEntry("LDAP://" + Common.domainName, Common.adminUsername, Common.adminPassword);
try
{
DirectorySearcher searcher = new DirectorySearcher(orgEntries);
// On filtre sur tous les objets de la catégorie OU
searcher.Filter = "(objectCategory=" + AD.Common.filterOrganizationalUnit + ")";
foreach (SearchResult result in searcher.FindAll())
{
// On récupère l'entrée trouvée lors de la recherche
DirectoryEntry DirEntry = result.GetDirectoryEntry();
// On récupère les infos
string name = (DirEntry.Properties["name"].Value != null) ? DirEntry.Properties["name"].Value.ToString() : String.Empty;
// On peut maintenant afficher les informations désirées
cbOu.Items.Add(name);
}
orgEntries.Close();
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
orgEntries.Close();
}
}
private string GetDistinguishedName(string categoryFilter, string searchedObjectName)
{
using (DirectoryEntry dnEntries = new DirectoryEntry("LDAP://" + Common.domainName, Common.adminUsername, Common.adminPassword))
{
try
{
DirectorySearcher searcher = new DirectorySearcher(dnEntries);
// On filtre sur tous les objets de la catégorie OU
searcher.Filter = "(objectCategory=" + categoryFilter + ")";
foreach (SearchResult result in searcher.FindAll())
{
// On récupère l'entrée trouvée lors de la recherche
DirectoryEntry DirEntry = result.GetDirectoryEntry();
// On récupère les infos
string name = (DirEntry.Properties["name"].Value != null) ? DirEntry.Properties["name"].Value.ToString() : String.Empty;
if (name == searchedObjectName)
{
string dn = (DirEntry.Properties["distinguishedName"].Value != null) ? DirEntry.Properties["distinguishedName"].Value.ToString() : String.Empty;
dnEntries.Close();
return dn;
}//if
}//foreach
return null;
}//try
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
dnEntries.Close();
return null;
}//catch
}//using
}
private void LoadGroups()
{
DirectoryEntry groupEntries = new DirectoryEntry("LDAP://" + Common.domainName, Common.adminUsername, Common.adminPassword);
try
{
DirectorySearcher searcher = new DirectorySearcher(groupEntries);
// On filtre sur tous les objets de la catégorie Groups
searcher.Filter = "(objectCategory=" + AD.Common.filterGroup + ")";
foreach (SearchResult result in searcher.FindAll())
{
// On récupère l'entrée trouvée lors de la recherche
DirectoryEntry DirEntry = result.GetDirectoryEntry();
// On récupère les infos
string name = (DirEntry.Properties["name"].Value != null) ? DirEntry.Properties["name"].Value.ToString() : String.Empty;
// On peut maintenant afficher les informations désirées
cbGroups.Items.Add(name);
}
groupEntries.Close();
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
groupEntries.Close();
}
}
private void CopyText(object sender, EventArgs e)
{
if (this.tbInitials.Text.Length < 1)
{
this.tbCommonName.Text = this.tbGivenName.Text + " " + this.tbSurname.Text;
}
else
{
this.tbCommonName.Text = this.tbGivenName.Text + " " + this.tbInitials.Text + ". " + this.tbSurname.Text;
}
}
private DirectoryEntry GetPath(string organizationalUnit)
{
try
{
string path = null;
if (organizationalUnit == null)
{
return new DirectoryEntry("LDAP://" + Common.domainName, Common.adminUsername, Common.adminPassword);
}//if
else if (organizationalUnit.Length > 0)
{
List<string> listOu = AD.ADActions.GetListOfOrganizationalUnits(GetDistinguishedName(Common.filterOrganizationalUnit, organizationalUnit));
if (listOu.Count > 0)
{
for (int i = 0; i < listOu.Count; i++)
{
path += "OU=" + listOu[i] + ",";
}//for
}//if
else if (listOu == null)
{
path = null;
}//else
return new DirectoryEntry("LDAP://" + path + Common.domainNameAsDN, Common.adminUsername, Common.adminPassword);
}//else
else return new DirectoryEntry("LDAP://" + path + Common.domainNameAsDN, Common.adminUsername, Common.adminPassword);
}//try
catch (Exception e)
{
throw new Exception("LDAP query impossible :\r\n", e);
}
}
}
} |
Partager