Click here to Skip to main content
15,920,688 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a Login page that has an error massage that fires when you enter the wrong password. How can I get it to fire when the user enters the wrong username and password?

C#
protected void Page_Load(object sender, EventArgs e)
    {

        if (IsPostBack)
        {
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
            con.Open();
            string cmdStr = "select count(*) from TableTrue where EmailAddress='" + TextBoxEA.Text + "'";
            
            SqlCommand userExist = new SqlCommand(cmdStr, con);
            SqlCommand cmd = new SqlCommand("select INST_ID, EmailAddress from TableTrue", con);
            int temp = Convert.ToInt32(userExist.ExecuteScalar().ToString());
            if (temp == 1)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Invalid Username/Password!!!')", true);
            }
        }

    }
Posted
Updated 12-Nov-13 9:09am
v2
Comments
Sergey Alexandrovich Kryukov 12-Nov-13 15:21pm    
Already a totally wrong ideas. Moving this way would be utterly unsafe. This is not how authentication is done.
—SA

The whole approach is wrong. You cannot determine if authentication data is correct in script. You are thinking like doing it on the client side, but everything on client side is accessible to all users, so your authentication can be faked in no time.

First, ideally, you should do authentication on a HTTPS page and thus leverage TLS:
http://en.wikipedia.org/wiki/HTTPS[^],
http://en.wikipedia.org/wiki/Transport_Layer_Security[^].

More importantly, not only the password should never be loaded on the client side, it should not be stored anywhere at all, not even on the server side. This is absolutely not needed for authentication and would be absolutely unsafe. Disagree, surprised? The see my past answers on the topic:
i already encrypt my password but when i log in it gives me an error. how can decrypte it[^],
Decryption of Encrypted Password[^],
storing password value int sql server with secure way[^],
TCP Connection with username and password[^].

These answers should explain to you why you need to use cryptographic hash function of a password: http://en.wikipedia.org/wiki/Cryptographic_hash_function[^].

—SA
 
Share this answer
 
Comments
Richard C Bishop 12-Nov-13 15:23pm    
Nice explanation. This should give clarification to the OP. +5.
Sergey Alexandrovich Kryukov 12-Nov-13 15:34pm    
Thank you, Richard.
—SA
You could change your query to this:
string cmdStr = "select count(*) from TableTrue where EmailAddress='" + TextBoxEA.Text + "' and INST_ID = " + TextBoxINST_ID.Text + "";


I only provided this code for ease of understanding. You should really be using parameterized queries to avoid SQL injection. Nonetheless, this query will check both user name and password and will only return data if both instances are satisfied.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Nov-13 15:42pm    
Sorry, this is a bad advise, in one aspect: doing so in unacceptable, due to SQL injection. Please see my Solution 3.
—SA
Richard C Bishop 12-Nov-13 15:44pm    
I completely agree. That is why I advised the OP to use parameterized queries. I only wanted to show the logic, if you will, of checking the username and password in a query.
Sergey Alexandrovich Kryukov 12-Nov-13 16:11pm    
Ah, sorry, I can see it now. Still, it could be confusing, as your unsafe code sample comes first and catches the eye. I was about to down-voted it. Well, it's good that I did not. :-)
Anyway, SQL injection and the use of parametrized statement is a delicate moment, that's why I decided to put a separate detailed answer on it. Many do this big mistake.
—SA
Your code and Solution 2 helped me to identify one more problem, also fatal to security. I'm talking about the query composed by concatenation with strings taken from UI. Not only repeated string concatenation is inefficient (because strings are immutable; do I have to explain why it makes repeated concatenation bad?), but there is way more important issue: it opens the doors to a well-known exploit called SQL injection.

This is how it works: http://xkcd.com/327[^].

What to do? Just read about this problem and the main remedy: parametrized statements: http://en.wikipedia.org/wiki/SQL_injection[^].

With ADO.NET, use this: http://msdn.microsoft.com/en-us/library/ff648339.aspx[^].

Please see my past answers for some more detail:
EROR IN UPATE in com.ExecuteNonQuery();[^],
hi name is not displaying in name?[^].

—SA
 
Share this answer
 

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

  Print Answers RSS


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