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
| using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Mobile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.MobileControls;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
using System.DirectoryServices.Protocols;
public partial class _Default : System.Web.UI.MobileControls.MobilePage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Command1_Click(object sender, EventArgs e)
{
if (RequiredFieldValidator1.IsValid && RequiredFieldValidator2.IsValid)
{
try
{
// Connexion à l'annuaire
DirectoryEntry Ldap = new DirectoryEntry("LDAP://nomdemondomaine", TextBox1.Text, TextBox2.Text);
// Nouvel objet pour instancier la recherche
DirectorySearcher searcher = new DirectorySearcher(Ldap);
// On modifie le filtre pour ne chercher que l'user dont le nom de login est dans textbox1
searcher.Filter = "(SAMAccountName=" + TextBox1.Text + ")";
// Pas de boucle foreach car on ne cherche qu'un user
SearchResult result = searcher.FindOne();
// On récupère l'objet trouvé lors de la recherche
DirectoryEntry DirEntry = result.GetDirectoryEntry();
this.ActiveForm = Form2;
Label7.Text = (string)DirEntry.Properties["givenName"].Value + " " + (string)DirEntry.Properties["sn"].Value;
}
catch (Exception ex)
{
RequiredFieldValidator2.IsValid = false;
RequiredFieldValidator2.ErrorMessage = "Echec d'ouverture de session";
}
}
}
protected void Command2_Click(object sender, EventArgs e)
{
if(CompareValidator2.IsValid && CustomValidator1.IsValid && RequiredFieldValidator3.IsValid && CustomValidator2.IsValid)
{
try
{
//Liaison à l’utilisateur administrateur membre du domaine
DirectoryEntry entry = new DirectoryEntry("LDAP://mondomaine", "loginadministrateur", "motdepasseadministrateur");
// Nouvel objet pour instancier la recherche
DirectorySearcher searcher = new DirectorySearcher(entry);
// On modifie le filtre pour ne chercher que l'user dont le nom de login est dans textbox1
searcher.Filter = "(SAMAccountName=" + TextBox1.Text + ")";
// Pas de boucle foreach car on ne cherche qu'un user
SearchResult result = searcher.FindOne();
// On récupère l'objet trouvé lors de la recherche
DirectoryEntry DirEntry = result.GetDirectoryEntry();
//Modification du mot de passe de l’utilisateur
DirEntry.Invoke("SetPassword", new object[] { TextBox5.Text });
//Destruction des objets
entry.Dispose();
this.ActiveForm = Form4;
}
catch (Exception ex)
{
CompareValidator2.IsValid = false;
CompareValidator2.ErrorMessage = "Votre mot de passe n'a pas pu etre changé";
}
}
}
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = (TextBox3.Text == TextBox2.Text);
}
protected void Command3_Click(object sender, EventArgs e)
{
this.ActiveForm = Form3;
}
protected void List1_ItemCommand(object sender, ListCommandEventArgs e)
{
}
protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = (TextBox4.Text.Length > 2);
}
} |
Partager