I'm trying to create a login page. I have managed to do the register page and store the email and the encrypted password but I'm struggling to do the login page to check the user exists and the password is correct. I think I've got it completely wrong but hoping some one will show me the correct code as I'm new this.
This is the stored procedure I created for logging in:
CREATE PROCEDURE [dbo].[Logged]
@Email NVARCHAR (50),
@Password NVARCHAR (50)
AS
BEGIN
SELECT *
FROM [dbo].[Register]
WHERE [Email] = @Email
AND [Password] = @Password
END
GO
Here is the login.aspx.cs code.
public string CheckPasswordQuery { get; private set; }
public string ToSHA2569(string value)
{
SHA256 sha256 = SHA256.Create();
byte[] hashData = sha256.ComputeHash(Encoding.Default.GetBytes(value));
StringBuilder returnValue = new StringBuilder();
for (int i = 0; i < hashData.Length; i++)
{
returnValue.Append(hashData[i].ToString());
}
return returnValue.ToString();
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
using (SqlConnection sqlcon = new SqlConnection(connectionString))
{
sqlcon.Open();
SqlCommand cmd = new SqlCommand("Logged", sqlcon);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Email", txtEmail.Text.Trim());
cmd.Parameters.AddWithValue("@Password", ToSHA2569(txtPassword.Text.Trim()));
cmd.ExecuteNonQuery();
if(CheckPasswordQuery == ToSHA2569(txtPassword.Text))
{
}
}
}
}
I'd be grateful if someone would be able to help me with this
UPDATE: is this code any closer? I really appreciate any help
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
using (SqlConnection sqlcon = new SqlConnection(connectionString))
{
string user = txtEmail.Text;
string pass = ToSHA2569(txtPassword.Text);
sqlcon.Open();
SqlCommand cmd = new SqlCommand("select @Email,@Password from [dbo].[Register] where Email=@Email and Password=@Password", sqlcon);
cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
cmd.Parameters.AddWithValue("@Password", ToSHA2569(txtPassword.Text));
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (txtEmail.Text == )
{
sqlcon.Close();
Response.Redirect("default.aspx");
}
else
{
sqlcon.Close();
}
}
}
catch (Exception ex)
{
lblWrong.Text = "Something went wrong please try again later";
}
}