hi all,
I am designing a login "component" for my application. Do you know of any good examples of login implementation on the web that I can get ideas from It must NOT be windows authenticated login. I have my own dB table etc.
Thanks
hi all,
I am designing a login "component" for my application. Do you know of any good examples of login implementation on the web that I can get ideas from It must NOT be windows authenticated login. I have my own dB table etc.
Thanks
Login
krazysmile
Nobita
Kishan
Hi
If you have your own database table that stores your user's credentials then you could use SQL and ADO.NET to authenticate your users. The following assumes that you have a table called "Users" with at least the fields "UserID, UserName, UserPassword":
private void btnLogin_Click(object sender, EventArgs e)
{
if (txtUserName.TextLength > 0 && txtPassword.TextLength > 0)
{
SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=test;Integrated Security=SSPI");
SqlDataAdapter adapter = new SqlDataAdapter("SELECT UserID FROM Users WHERE UserName='" + txtUserName.Text + "' AND UserPassword='" + txtPassword.Text + "'", connection);
DataTable dt = new DataTable("Users");
adapter.Fill(dt);
if(dt.Rows.Count == 0)
{
MessageBox.Show("Invalid User Details", "Login");
txtUserName.ResetText();
txtPassword.ResetText();
}
else
{
// login validated.
MessageBox.Show("Login successful", "Login");
}
}
}
Also, I used the SQLClient namespace (using System.Data.SqlClient directive) but if you are using Access or another data source then you can change to using the OleDb namespace or other relevant namespace.
HTH
Michelle Baumgarten
Youngmin
Do you actually make the column in the db an encrypted value Or do you encrypt it in code and then send the encryped value to the db
Could you please give an example
abeljda
Use paramitrimized queries. Then you never have to worry about format's or SQL Injection.
It's olso better for the preformance, because you don't need to have to concatenate a string for example:
string query = "SELECT * FROM Table1 WHERE ID = " + txtId.Text + " AND Name = \"" + "txtName.Text + "\"";
No escape characters needed, you doesn't have to think about using a " or not etc.
Parameters are like placeholders, you use them in Stored Procedures as well. A little example:
// TODO: Set date variable.
DateTime date = DateTime.Now;
// Set query and parameters.
const string query = "SELECT * FROM Table1 WHERE MyDate = @MyDate";
SqlParameter pMyDate = new SqlParameter("@MyDate", SqlDbType.DateTime);
pMyDate.Value = date;
// Create connection and open it.
SqlConnection dbConn = new SqlConnection("ConnectingString");
dbConn.Open();
try
{
using(SqlCommand dbCommand = new SqlCommand(query, dbConn))
{
// Add paramter to Command.
dbCommand.Parameters.Add( pMyDate );
// Execute the query and get results.
SqlDataReader reader = dbCommand.ExecuteReader();
try
{
// Walkthrough results.
while(reader.Read())
{
// TODO: Do something with the data.
}
}
finally
{
// Close reader.
reader.Close();
}
}
}
finally
{
// Close connection.
dbConn.Close();
}
Eli Cohen