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
| using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private bool Authentifier(string strUtilisateur, string strMotDePasse)
{
bool bOk = false;
// Cryptage du mot de passe
strMotDePasse = FormsAuthentication.HashPasswordForStoringInConfigFile(strMotDePasse, "MD5");
// Création d'une connexion SGBD
SqlConnection oConnexion = new SqlConnection("user id=sa;password=;initial catalog=pubs;data source=pttravail");
// Définition de la requête à exécuter
SqlCommand oCommand = new SqlCommand("SELECT * FROM Utilisateurs WHERE nom='" + strUtilisateur + "'", oConnexion);
try
{
// Ouverture de la connexion et exécution de la requête
oConnexion.Open();
SqlDataReader drUtilisateur = oCommand.ExecuteReader();
// Parcours de la liste des utilisateurs
while (drUtilisateur.Read())
{
if (drUtilisateur["motdepasse"].ToString() == strMotDePasse)
{
bOk = true; break;
}
}
}
catch
{
bOk = false;
}
oConnexion.Close();
return bOk;
}
private void BtConnexion_Click(object sender, System.EventArgs e)
{
if (Authentifier(txtUtilisateur.Text, txtMotDePasse.Text))
{
FormsAuthentication.RedirectFromLoginPage(txtUtilisateur.Text, false);
}
else
{
lbMessage.Text = "Erreur d'authentification, l'utilisateur ou le mot de passe n'existent pas!";
}
}
private void BtDeconnexion_Click(object sender, System.EventArgs e)
{
FormsAuthentication.SignOut(); Response.Redirect("Login.aspx");
}
} |
Partager