One way to prevent SQL Injection in qurestring Mode
Last Update:2018-12-07
Source: Internet
Author: User
Public Static String Saferequest ( String Str)
{
String Outstr = Null ;
Object Querstr = Httpcontext. Current. Request. querystring [STR];
If (Querstr ! = Null )
{
Outstr = Inputtext (querstr. tostring (), 30 );
Return Outstr;
}
Else
Return Outstr;
}
Public Static String Inputtext ( String Inputstring, Int Maxlength)
{
System. Text. stringbuilder retval = New System. Text. stringbuilder ();
// Check incoming parameters for null or blank string
If (Inputstring ! = Null ) && (Inputstring ! = String. Empty ))
{
Inputstring = Inputstring. Trim ();
// Op the string incase the client-side max length
// Fields are bypassed to prevent buffer over-runs
If (Inputstring. Length > Maxlength)
Inputstring = Inputstring. substring ( 0 , Maxlength );
// Convert some harmful symbols incase the regular
// Expression validators are changed
For ( Int I = 0 ; I < Inputstring. length; I ++ )
{
Switch (Inputstring [I])
{
Case ' " ' :
Retval. append ( " & Quot; " );
Break ;
Case ' < ' :
Retval. append ( " & Lt; " );
Break ;
Case ' > ' :
Retval. append ( " & Gt; " );
Break ;
Default :
Retval. append (inputstring [I]);
Break ;
}
}
// Replace single quotes with white space
Retval. Replace ( " ' " , " " );
Retval. Replace ( " ; " , " " );
Retval. Replace ( " Insert " , "" );
Retval. Replace ( " Select " , "" );
Retval. Replace ( " Delete " , "" );
Retval. Replace ( " Update " , "" );
Retval. Replace ( " Drop " , "" );
Retval. Replace ( " Create " , "" );
Retval. Replace ( " Alter " , "" );
Retval. Replace ( " " , " 20% " );
Retval. Replace ( " Xp_mongoshell " , "" );
Retval. Replace ( " Xp_regaddmultistring " , "" );
Retval. Replace ( " Xp_regdeletekey " , "" );
Retval. Replace ( " Xp_regdeletevalue " , "" );
Retval. Replace ( " Xp_regenumkeys " , "" );
Retval. Replace ( " Xp_regenumvalues " , "" );
Retval. Replace ( " Xp_regread " , "" );
Retval. Replace ( " Xp_regremovemultistring " , "" );
Retval. Replace ( " Xp_regwrite " , "" );
Retval. Replace ( " Sp_oacreate " , "" );
Retval. Replace ( " Sp_oadestroy " , "" );
Retval. Replace ( " Sp_oamethod " , "" );
Retval. Replace ( " Sp_oagetproperty " , "" );
Retval. Replace ( " Sp_oasetproperty " , "" );
Retval. Replace ( " Sp_oageterrorinfo " , "" );
Retval. Replace ( " Sp_oastop " , "" );
}
Return Retval. tostring ();
}