Click here to Skip to main content
15,918,049 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I need query or c# code to Compare between Hashed password and numeric Password in manually login page

What I have tried:

SqlCommand cmd = new SqlCommand("select aspnet_Users.id, aspnet_Membership.Password from aspnet_Users,aspnet_Membership where id= '" + TextBox1.Text + "'AND Password= '" + TextBox2.Text + "'", conn);
Posted
Updated 3-Mar-20 3:21am

As previously stated; your code is ripe with an SQL Injection Vulnerability.
NEVER EVER string together and Sql Command with user input, you should always use Parameters to add user content to a command. This also has the benefit that the SQL Data Provider will set the types (INT, VARCHAR, DATE) automatically based on the content being provided (as long as it is not NULL).

The way I would write the code out would be to follow this order of operations:
1. SQL Command to retrieve the Password based on the UserName.
2. Execute Scalar the command- if no return the UserName was not found.
3. Use whatever Hash function was applied to the saved password on the return
4. Compare the hashed version of the password attempt to what was saved in the DB

This is the basic structure of what this would look like. You will need to adjust the code accordingly for your table structure and functions
(Sorry I do not have the time to research what you have in use and this keyboard is bad so I have omitted some characters/brackets)
C#
SqlCommand cmd = new SqlCommand("SELECT PasswordHash FROM AccountTable WHERE UserName = @UserName", conn);
cmd.Parameters.AddWithValue("UserName", TextBox1.Text);

conn.Open();
var SqlReturn = cmd.ExecuteScalar();

if (SqlReturn == null)
	// account not found
else
	string PasswordAttempt = HashFunctionName(TextBox2.Text); // whatever this function is called
	if (PasswordAttempt != SqlReturn.ToString() 
		// login failed
	else
		// Success
 
Share this answer
 
Quote:
I need query or c# code to Compare between Hashed password and numeric Password in manually login page

You need to apply same hashing function as the 1 applied to existing passwords.
Hash function - Wikipedia[^]
C#
SqlCommand cmd = new SqlCommand("select aspnet_Users.id, aspnet_Membership.Password from aspnet_Users,aspnet_Membership where id= '" + TextBox1.Text + "'AND Password= '" + TextBox2.Text + "'", conn);

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 
You're using the ASP.NET membership provider, so you don't need to validate the password yourself; just call the Membership.ValidateUser[^] method.
C#
bool isPasswordCorrect = Membership.ValidateUser(TextBox1.Text, TextBox2.Text);
NB: You should give your controls meaningful names, rather than just accepting the default names assigned by the Visual Studio designer. Sure, you might remember what value TextBox42 represents now, but when you come back to your code in a few weeks, you won't.
 
Share this answer
 
Two things wrong there:
1. Never use string concatenation to create SQL commands, as it leaves your database vulnerable to SQL injection. Use proper parameterised queries.
2. Do not pass a clear text password in as one of the query parameters. You should first create the hash of the entered text and compare the two hash values.
 
Share this answer
 
Comments
Member 13058758 2-Mar-20 5:12am    
how I create the hash of the entered text?
Richard MacCutchan 2-Mar-20 5:17am    
Exactly the same way that you created the original hash.
Member 13058758 2-Mar-20 5:21am    
I make it in configuration file passwordFormat="Hashed"
Richard MacCutchan 2-Mar-20 6:11am    
Sorry I don't understand; which configuration file, and what password are you referring to?
Member 13058758 2-Mar-20 6:23am    
<membership>
<providers>
<clear>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionstringname="localsqlserver" enablepasswordretrieval="false" enablepasswordreset="true" requiresquestionandanswer="false" requiresuniqueemail="false" passwordformat="Hashed" minrequirednonalphanumericcharacters="0" minrequiredpasswordlength="3" maxinvalidpasswordattempts="3" passwordattemptwindow="15">



this my code in web.config file that I make to my allow roles in web application
and I insert identity column and called it id in database now i need c# sqlcommand to compare between hashed password and actual password where this id and password is true

*I make login form enter this id and password instead of username and password

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900