c# - How to use Linq instead of SQL Injection query for custom search -


i use linq instead of below hardcoded sql injection search sqlserver database tables. how retrieve dynamically generated web controls input texts in c# linq , replace entire sql injection in linq searching.

my c# code:

protected void search_button_click(object sender, eventargs e)  {     try       {          table maintable = select.findcontrol("dynamic_filter_table_id") table;        int rc = maintable.rows.count;             if (rc == 1)             {                  dropdownlist d1 = maintable.findcontrol("mainddl") dropdownlist;                 if (d1.selectedvalue.contains("decimal"))                 {                     textbox t1 = maintable.findcontrol("txtbox1") textbox;                     textbox t2 = maintable.findcontrol("txtbox2") textbox;                     sqldataadapter sql = new sqldataadapter("select f.col1,f.col2,v.col1, col2,col3, col4 , col5, cl6 table1 v , tabl2 f v.col1 = f.col1 , " + ddl1.selecteditem.text + " >= " + t1.text + " , " + ddl1.selecteditem.text + " <= " + t2.text, con);                      dataset data = new dataset();                     sql.fill(data);                     con.close();                     session["dataforsearch_ddl"] = data.tables[0];                 }             }        }      catch     {       impropersearch();     } } 

why don't rewrite query sql-injection safe? linq won't give benefit.

you can achieve doing 2 things.

the first secure column names. accomplished specifying characters allowed column names (more secure trying figure out characters not allowed). in case remove letters , digits. if have column names contains underscore, add check.

the next thing use parameterized queries. each ado.net driver has built in support that, have specify value using cmd.parameters.addwithvalue. doing value isn't part of query string , hence there no potential sql injection.

using (var con = yourconnectionfactory.create()) {     using (var cmd = new sqlcommand(con))     {         var safekey1 = onlylettersanddigits(ddl1.selecteditem.text);         var safekey2 = onlylettersanddigits(ddl2.selecteditem.text);          cmd.commandtext = "select f.col1,f.col2,v.col1, col2,col3, col4 , col5, cl6 " +                                " table1 v , tabl2 f v.col1 = f.col1 " +                               " , " + safekey1 + " >= @text1 " +                                " , " + safekey2 + " <= @text2 ";             cmd.parameters.addwithvalue("text1", t1.text);             cmd.parameters.addwithvalue("text2", t2.text);              var adapter = new sqldataadapter(cmd);              var data = new dataset();             sql.fill(data);             session["dataforsearch_ddl"] = data.tables[0];     } }  public string onlylettersanddigits(string value) {     var stripped = "";     foreach (var ch in value)     {         if (char.isletterordigit(ch))             stripped += ch;     }      return stripped; } 

Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -