Currently I trying to allow people to reset there user password. My code seems to working fine, I am able to query the database, and also send Emails with password, my only issue I am having is running the update Command query within the data Reader Also how do I Hash a plain Text Password in an SQL Query.
Thanks
below is my code:
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
string connetionString = null;
string sqlupdate = null;
string sqlCheckUser = null;
string sqlCheck = null;
string user = username.Text;
string password = password_row.Text;
//Simple Text Field Vaildator
if (user == String.Empty)
{
required.Text = "Please Enter Username";
return;
}//end if
else
{
SqlConnection cnn;
SqlCommand command;
string ConnectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
sqlCheckUser = "SELECT * FROM Users WHERE LoginID='" + user + "'";
sqlupdate = "UPDATE Users SET Password='" + password + "' WHERE LoginID='" + user + "'";
cnn = new SqlConnection(ConnectionString);
try
{
cnn.Open();
using (command = new SqlCommand(sqlCheckUser, cnn))
{
SqlDataReader dataReader = command.ExecuteReader(); //Not used
while (dataReader.Read())
{
string email = (string)dataReader["Email"];
string userName = (string)dataReader["LoginID"];
TextBox1.Text = Convert.ToString(email); //Testing OutPut
required.Text = "Please Check your Email Address for New Password";
if (userName == user)
{
SqlCommand update = new SqlCommand(sqlupdate, cnn);
required.Text = "Please Check your Email Address for New Password";
/*
* Send Email to User with New Password.
*/
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("");
mail.From = new MailAddress("");
mail.To.Add(email);
mail.Subject = "FTP Password Reset";
mail.Body = "The Password for your FTP account has been reset. Your new password is the following: " + password;
SmtpServer.Send(mail);
/*
* End of Email
**/
}//End If
}//End While
}//End Using
command.Dispose();//Dispose of Command
cnn.Close();//Close Database Connection
}//End Try
catch (Exception ex)
{
TextBox1.Text = Convert.ToString("Can not open connection ! ");//Output on Connection
}//End Catach
}//End Else
}//End Button on Click
commandParameters in queries. This code is open to sql injections in so many ways.