C # prevents SQL injection from being reproduced

Source: Internet
Author: User

C # prevents SQL injection attackshttp://blog.csdn.net/limlimlim/article/details/8629582 L Login Judgment: SELECT * from T_users where username= ... and password= ..., the parameters are spelled into the SQL statement. L Construct a malicious password:hello ' or 1=1-- L if (Datareader.read ()) L { l MessageBox.Show ("landing Success"); L} L Else L { l MessageBox.Show ("Landing failed"); L} l Prevention of injection vulnerability attacks: Do not use SQL statement stitching, parameter assignment

String constr = "Data source=zxtiger;initial catalog=itcastcn;integrated security=true";
using (SqlConnection con = new SqlConnection (CONSTR))
{
String sql = "SELECT COUNT (*) from Userlogin where[email protected]and[email protected]";
using (SqlCommand cmd = new SqlCommand (sql, con))
{
Tell the Command object before execution @uid and @pwd who will replace it in the future.
Assigning values to variable @uid and @pwd
Cmd. Parameters.addwithvalue ("@uid", TxtUid.Text.Trim ());
Cmd. Parameters.addwithvalue ("@pwd", Txtpwd.text);
#region Myregion
SqlParameter p1 = new SqlParameter ("@uid", TxtUid.Text.Trim ());
Cmd. Parameters.Add (p1);

                    //sqlparameter p2 = new SqlParameter ("@pwd", Txtpwd.text);
                    //cmd. Parameters.Add (p2);
                     #endregion


sqlparameter[] PMS = new sqlparameter[] {
New SqlParameter ("@uid", TxtUid.Text.Trim ()),
New SqlParameter ("@pwd", Txtpwd.text)
};
Cmd. Parameters.addrange (PMS);


Con. Open ();
int r = Convert.ToInt32 (cmd). ExecuteScalar ());
Con. Close ();
if (R > 0)
{
MessageBox.Show ("Login Successful! ");
}
Else
{
MessageBox.Show ("Login failed! ");
}
}
}

C # prevents SQL injection from being reproduced

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.