Recently in one of my projects, i faced a challenge to validate the character set that can be input in to a Telerik Combo Box. Initially any invalid character that i entered such as "<" would cause the Combo box to crash, because such characters are illegal, and the crash comes because they are sent at the server side (ItemsRequsted Event).
I started on with my search but could not find anything conclusive that could do the job for me. In the end after going through their client API i found the function of my interest "OnClientItemsRequesting". This function has the ability to cancel the server side event if it returns false. So i wrote the following code in my aspx page.
The ComboBox will now call the ClearComboItems function when OnClientItemsRequesting is fired. The definition for ClearComboItems is belowI started on with my search but could not find anything conclusive that could do the job for me. In the end after going through their client API i found the function of my interest "OnClientItemsRequesting". This function has the ability to cancel the server side event if it returns false. So i wrote the following code in my aspx page.
<radC:RadComboBox ID="ComboBox" CausesValidation="false"
ExpandEffect="Stretch" AutoPostBack="false" runat="server"
EnableLoadOnDemand="True" Height="200px"
OnItemsRequested="RadComboBox1_ItemsRequested"
AllowCustomText="true"
MarkFirstMatch="True" Width="230px" TabIndex="1"
OnClientItemsRequesting="ClearComboItems"
MaxLength="50"></radC:RadComboBox>
function ClearComboItems(combobox)As we can see above, we have created an array badkeys and defined all the characters that have to be deprecated from being sent to the server. The return false argument does not let the sever side event to be fired and the illegal string are not passed to the server. To put in a place a check at the server you can add the same logic to the ComboBox_ItemsRequested event. This is shown below
{
badkeys= new Array("<", "%3c", "<", "%3C", "%3e",
"%3E", "%27", "'", """, "&","'",
"<", ">", """, "'", "&",
"<", ">", ""e;", "'",
"~","@","#","$","^","&","*",":","!","`","&",
"<", ">", "\\u0022", "\\u0026", "\\u0027",
"\\u003c", "\\u003e", "select", "insert","update",
"delete", "drop", "having", "truncate", "union",
"--", "#", "%", "@@", "+","_","-","=","1","2","3","4",
"5","6","7","8","9","0", "1=1","exec", "alert", ";", "document",
"meta","iframe","html","script", "0x", "{","<", ">", "xp_",
"'1'='1'", "../", "./", ".exe", ".dll", "/*","*/", "waitfor",
"&#", "<<", ".config");
var val = combobox.GetText();
for (i=0; i<val.length; i++)
{
for (j=0; j<badkeys.length; j++)
{
if (val.indexOf(badkeys[j],i) == -1)
{
}
else
{
combobox.ClearSelection();
return false;
}
}
}
}
protected void RadComboBox1_ItemsRequested(object o, Telerik.WebControls.RadComboBoxItemsRequestedEventArgs e)public static string ClearComboItems(string strText)
{
string key = ClearComboItems(e.Text.ToString());
}
{To further strengthen the security of the Combo box you may also call the same function on "OnClientBlur". This will avoid copy pasting of illegal characters in the combo box.
string[] badkeys = {"<", "%3c", "<", "%3C",
"%3e", "%3E", "%27", "'", """,
"&", "'", "<", ">", """, "'", "&",
"<", ">", ""e;", "'"
, "~", "@", "#", "$", "^", "&", "*", ":", ".", "!","`",
"&", "<", ">", "\\u0022","\\u0026", "\\u0027",
"\\u003c", "\\u003e", "select", "insert", "update", "delete",
"drop", "having","truncate", "union", "--", "#", "%", "@@", "+",
"_", "-", "=","1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
"'", "1=1", "exec", "alert", ";","document", "meta", "iframe",
"html", "script", "0x", "{", "<", ">", "xp_", "'1'='1'", "../",
"./", ".exe", ".dll", "/*", "*/", "waitfor", "&#", "<<", ".config"};
for (int i = 0; i < strText.Length; i++)
{
for (int j = 0; j < badkeys.Length; j++)
{
strText = strText.Replace(badkeys[j],"");
}
}
return strText.Trim();
}
I hope this helps..
No comments:
Post a Comment