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
| public partial class login : System.Web.UI.Page
{
static private string GetConnectionString()
{
// To avoid storing the connection string in your code,
// you can retrieve it from a configuration file, using the
// System.Configuration.ConfigurationSettings.AppSettings property
return @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True";
}
string urlSqlServer1 = GetConnectionString();
protected void Page_Load(object sender, EventArgs e)
{
if (Request.IsAuthenticated)
{
FormsAuthentication.RedirectFromLoginPage(User.Identity.Name,true);
}
}
private bool Authentifier(string strUtilisateur, string strMotDePasse)
{
bool bOk = false;
// chemin vers la BD (SQL serveur 2005):
string urlSqlServer = GetConnectionString();
try
{
// objet de connexion :
SqlConnection connexion;
connexion = new SqlConnection(urlSqlServer);
// ouvrir la connexion :
connexion.Open();
// préparation de la requête effective :
string Requete = "SELECT * FROM users WHERE nom='" + strUtilisateur + "'";
SqlCommand cmdSqlServ = new SqlCommand(Requete, connexion);
//lecture de plusieurs lignes :
SqlDataReader lignesRead;
lignesRead = cmdSqlServ.ExecuteReader();
// Parcours de la liste des utilisateurs
while (lignesRead.Read())
{
if (lignesRead["motdepass"].ToString() == strMotDePasse)
{
bOk = true; break;
}
}
// fermer la connexion :
connexion.Close();
}
catch
{
bOk = false;
}
return bOk;
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Request.IsAuthenticated)
{
User.IsInRole("client");
}
if (Authentifier(txtUtilisateur.Text,txtMotDePasse.Text))
{
FormsAuthentication.RedirectFromLoginPage(txtUtilisateur.Text, true);
}
else
{
lbMessage.Text = "Erreur d'authentification, l'utilisateur ou le mot de passe n'existent pas!";
}
}
protected void Button3_Click(object sender, EventArgs e)
{
FormsAuthentication.SignOut(); Response.Redirect("Login.aspx");
}
protected void Button1_Load(object sender, EventArgs e)
{
} |
Partager